Prime Game – Solution – Java | Techgig – CodeGladiator 2021

Prime Game 

Rax, a school student, was bored at home in the pandemic. He wanted to play but there was no one to play with. He was doing some mathematics questions including prime numbers and thought of creating a game using the same. After a few days of work, he was ready with his game. He wants to play the game with you.

GAME:

Rax will randomly provide you a range [ L , R ] (both inclusive) and you have to tell him the maximum difference between the prime numbers in the given range. There are three answers possible for the given range.

  1. There are two distinct prime numbers in the given range so the maximum difference can be found.
  2. There is only one distinct prime number in the given range. The maximum difference in this case would be 0.
  3. There are no prime numbers in the given range. The output for this case would be -1.
  4. Advertisement

To win the game, the participant should answer the prime difference correctly for the given range.

Example:

Range: [ 1, 10 ]

The maximum difference between the prime numbers in the given range is 5.

Difference = 7 – 2 = 5

Range: [ 5, 5 ]

There is only one distinct prime number so the maximum difference would be 0.

Range: [ 8 , 10 ]

There is no prime number in the given range so the output for the given range would be -1.

Can you win the game?

Input Format

The first line of input consists of the number of test cases, T

Next T lines each consists of two space-separated integers, L and R

Constraints

1<= T <=10

2<= L<= R<=10^6

Output Format

For each test case, print the maximum difference in the given range in a separate line.

Sample TestCase 1

Input

55 52 78 1010 204 5 Output 05-180

Test Case 1: [ 5 – 5 ] = 0

Test Case 2: [ 7 – 2 ] = 5

Test Case 3: No prime number in the given range. Output = -1

Test Case 4: [ 19 – 11 ] = 8
Test Case 5: The difference would be 0 since there is only one prime number in the given range.

/* Read input from STDIN. Print your output to STDOUT*/

import java.io.*;
import java.util.*;
public class CandidateCode {
   public static void main(String args[] ) throws Exception {

        Scanner scanner = new Scanner(System.in);
        int count = scanner.nextInt();
        int counter = 0;
        while(counter < count){
            counter++;
            int first = scanner.nextInt();
            int last = scanner.nextInt();
            System.out.println(getMaxDiff(first, last));
        }
   }

   public static int getMaxDiff(int first, int last){
      int start = 0; int end = -1;
      for(int i=first;i<=last;i++){
         if(isPrime(i)){
            start=i;
            break;
         }
      }
      for(int i=last;i>=first;i--){
         if(isPrime(i)){
            end=i;
            break;
         }
      }
      return end - start;
   }

   static boolean isPrime(long n) {
    if(n < 2) return false;
    if(n == 2 || n == 3) return true;
    if(n%2 == 0 || n%3 == 0) return false;
    long sqrtN = (long)Math.sqrt(n)+1;
    for(long i = 6L; i <= sqrtN; i += 6) {
        if(n%(i-1) == 0 || n%(i+1) == 0) return false;
    }
    return true;
   }

}
Advertisement

Leave a Reply

Your email address will not be published. Required fields are marked *

nine − 1 =