**Question 2**: **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.

There are two distinct prime numbers in the given range so the maximum difference can be found.

There is only one distinct prime number in the given range. The maximum difference in this case would be 0.

There are no prime numbers in the given range. The output for this case would be -1.

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:**

5

5 5

2 7

8 10

10 20

4 5

**Output:**

0

5

-1

8

0

**Explanation**

Test Case 1: [ 5 – 2 ] = 3

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.

Time Limit(X):

0.50 sec(s) for each input.

Memory Limit:

512 MB

Source Limit:

100 KB

Allowed Languages:

C, C++, C++11, C++14, C#, Java, Java 8, Kotlin, PHP, PHP 7, Python, Python 3, Perl, Ruby, Node Js, Scala, Clojure, Haskell, Lua, Erlang, Swift, VBnet, Js, Objc, Pascal, Go, F#, D, Groovy, Tcl, Ocaml, Smalltalk, Cobol, Racket, Bash, GNU Octave, Rust, Common LISP, R, Julia, Fortran, Ada, Prolog, Icon, Elixir, CoffeeScript, Brainfuck, Pypy, Lolcode, Nim, Picolisp, Pike, pypy3.

**Solution:**

**Python 3.8**

''' Read input from STDIN. Print your output to STDOUT ''' #Use input() to read input from STDIN and use print to write your output to STDOUT import math def check(n): if n>1: for i in range(2, int(math.sqrt(n))+1): if n%i == 0: return False return True def main(): n=int(input()) for i in range(n): a,b = input().split(" ") a=int(a) b=int(b) temp=a x=[] while(a<=b): if check(a): x.append(a) break a+=1 a=temp while(a<=b): if check(b): x.append(b) break b-=1 if len(x)==0: print(-1) else: print(max(x)-min(x)) main() ''' End '''

**-Proof of Working-**

**Follow us**

**Also Checkout**

- Elementor #14055
- HPE Off Campus Recruitment Drive | Software Engineer | 2022 2021 | Codewindow.in
- Yash Technologies Hiring | Trainee Programmer-Java | Codewindow.in
- Trianz Off Campus Hiring Drive | Graduate Engineer trainee | Codewindow,in
- Remainder mod 11 | Accenture Solve
- Non Duplicate String | Accenture Solve
- Autobiography Number Solution | Codewindow.in
- Count kth Digit Solution | Codewindow.in
- Revature Interview Experience | Part 1 | Codewindow.in
- Persistent Interview Experience | Codewindow.in
- L&T Off Campus Drive 2021 | Senior Engineer | 2019/ 2020/2021 | Codewindow.in
- Cognizant GenC Recruitment Drive | Programmer Trainee | Codewindow.in
- Amdocs Off Campus Hiring | System Analyst | Codewindow.in
- Zoho Off Campus Hiring | Software Developer | 2018/ 2019/ 2020/2021 | Codewindow.in
- Kumaran Systems Off Campus Hiring | Trainee Programmer | 2020 2019 | Codewindow.in
- Accenture ASE Interview Experience
- CodeLogicX Recruitment Drive | Software Developer Trainee | 2021, 2021 | Codewindow.in
- Accenture SE Interview Experience
- Juspay Hiring Challenge | Think big with Functional Programming | Codewindow.in
- Exam Answers
- IBM Off Campus Hiring | Associate System Engineer | Codewindow.in
- FindMax Problem
- Walmart Off Campus Hiring | Walmart CodeHers | Codewindow.in
- Quadratic Equation Solution | Codewindow.in
- Accenture Solution | Code in Marathon | Codewindow.in
- Amazon Off Campus Internship Drive | Amazon WoW for the batch of 2022, 2023, and 2024 | Codewindow.in
- Binary Tree Combination
- Sum of all the prime divisors of a number
- Mindtree Interview Questions and Experience Part 1 | Codewindow.in
- Apisero Off Campus Recruitment Drive | Software Engineer | 2021 2020 2019 2018 | Codewindow.in
- Infosys Mega Off Campus HIring | Technincal Process Executive | Codewindow.in
- Amazon Off Campus Hiring | Software Development Engineer | 2021/2020/2019/2018 | Codewindow.in
- Question 3 Solution | Duplicate Elements Solution | Codewindow.in
- Question 2 Solution | Second Largest Solution | Codewindow.in
- Question 1 Solution | Palindrome Solution | Codewindow.in
- Question 2 Coding Solution | Weak String Problem | Codewindow.in
- Question 1 Coding Solution | Permutation Problem | Codewindow.in
- XL Dynamics Off Campus Hiring | 2022 2021 2020 | Codewindow.in
- Accenture Full Question Paper Set | Codewindow.in
- Accenture Aptitude Questions and Solution | Codewindow.in
- Accenture Logical Reasoning Questions and Solution | Codewindow.in
- MountBlue Solution | PlayGame Code | Codewindow.in
- MountBlue Solution | !On Off Code | Codewindow.in
- Revature Answer | 2021
- IBM Mixed Fraction Code Solution | CodeWindow.in
- IBM Volume of Pizza Slice Solution | CodeWindow.in
- Accenture Running Distance Code Solution
- Accenture Pizza Code Solution
- Cognizant GENC Off-Campus | 2021 | CodeWindow.in
- Revature Coding Solution with proof | Array Operation: Ascending order | Codewindow.in

- June 2021 (62)
- May 2021 (85)
- April 2021 (173)
- March 2021 (145)
- January 2021 (4)
- December 2020 (3)
- November 2020 (14)
- October 2020 (35)
- September 2020 (15)