
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
- Dunzo Off-Campusing Drive | Data Engineer | codewindow.in
- Verbal Ability Questions Solved | Synonyms Antonyms – codewindow.in
- Verbal Ability Questions Solved | Statement Correction – codewindow.in
- Verbal Ability Questions Solved | Spotting Error – codewindow.in
- Verbal Ability Questions Solved | Speech and Tenses – codewindow.in
- Try
- Verbal Ability Questions Solved | Sentence Arrangement – codewindow.in
- Verbal Ability Questions Solved | Prepositions – codewindow.in
- Verbal Ability Questions Solved | Grammar – codewindow.in
- Verbal Ability Questions Solved | Articles – codewindow.in
- Zoho Off Campus Drive | Product Marketing Associate / Specialist | codewindow.in
- BerryWorks Off Campus Recruitment Drive – Software Engineer – Codewindow.in
- Microsoft Off Campus Hiring Drive – Technical Support – Codewindow.in
- Infosys Pseudocede & Puzzle Solved – codewindow.in
- Infosys Verbal Questions Solved – codewindow.in
- Amazon Off Campus Drive | Trainee | codewindow.in
- Hawkins Off Campus Drive | Management trainees | codewindow.in
- Infosys Mathematical Questions Solved – codewindow.in
- Paypal Off Campus Recruitment Drive – Software Engineer – Codewindow.in
- IBM Off Campus Hiring Drive – Associate Systems Engineer – Codewindow.in
- Infosys Off Campus Hiring Drive – SP and DSE – Codewindow.in
- VIRTUSA Recruitment 2022 | Associate Engineer | codewindow.in
- Infosys Logical Questions solved – codewindow.in
- Google Off Campus Hiring Drive – IT Support Engineer – Codewindow.in
- Capgemini Pseudocode Solved | Set 4 – codewindow.in
- Capgemini Pseudocode Solved | Set 3 – codewindow.in
- Capgemini Pseudocode Solved | Set 2 – codewindow.in
- JECA 2022 Question Paper – 90+ Sample Questions Exposed – codewindow.in
- Capgemini Pseudocode Solved | Set 1 – codewindow.in
- Verizon Off Campusing | Software Developer | codewindow.in
- TCS Recruitment Off-Campusing | Service Desk Analyst | codwindow.in
- Amdocs Off-Campusing | Devops Engineer | codewindow.in
- Wipro Paper Solved | Technical Set 2 – codewindow.in
- Wipro Paper Solve | Technical Set 1 – codewindow.in
- Mindtree Off-Campusing | Software Engineer | codewindow.in
- Mastercard Off Campus Hiring Drive – Jr Software Engineer – Codewindow.in
- Wipro Paper Solve | Aptitude Set 3 – codewindow.in
- MindTree Interview Round Experience – 30+ Questions EXPOSED – codewindow.in
- Wipro Paper Solve | Aptitude Set 2 – codewindow.in
- Wipro Paper Solve | Aptitude Set 1 – codewindow.in
- Verbal Ability | Set 4 | Infosys – codewindow.in
- Wipro Off-Campusing | Project Engineer | codewindow.in
- TCS Digital Coding Questions – 3 Problems with Solution – codewindow.in
- Capgemini Placement Questions 2022 – 20 Frequent Questions Solved – codewindow.in
- Accenture Paper on 10th December – All Questions Solved – codewindow.in
- Flipkart GriD 4.0 | Software Development Challenge 2022 | codewindow.in
- Axis Bank Off-Campusing | Branch Relationship Officer | codewindow.in
- Mundrisoft Off-Campusing | Trainee DevOps Engineer | codewindow.in
- Verbal Ability | Set 3 | Infosys – codewindow.in
- Verbal Ability | Set 2 | Infosys – codewindow.in
- June 2022 (56)
- May 2022 (96)
- April 2022 (72)
- March 2022 (31)
- February 2022 (28)
- January 2022 (26)
- December 2021 (25)
- November 2021 (94)
- October 2021 (64)
- September 2021 (55)
- August 2021 (19)
- July 2021 (29)
- June 2021 (103)
- May 2021 (85)
- April 2021 (173)
- March 2021 (145)
- January 2021 (4)
- December 2020 (3)
- November 2020 (14)
- October 2020 (35)
- September 2020 (15)