
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
- UKG Hiring for Software Engineer
- Unlocking Innovation and Diversity: Accenture HackDiva Empowers Women in Tech with Cutting-Edge Solutions – codewindow.in
- QA Engineer Opportunities at Siemens Company: Apply Now – codewindow.in
- Transform Your Career with S&P Global: Apply for the Software Development Engineer Role and Lead the Future of Financial Technology Innovation – codewindow.in
- Unlock Your Potential at Accenture as an Associate Software Engineer – Elevate Your Career with Innovation and Excellence – codewindow.in
- Accelerate Your Career: Join NVIDIA’s Elite Software Engineering Internship Program and Shape the Future of Technology – codewindow.in
- Software Engineer at ABB: Unlock Innovation and Shape the Future – codewindow.in
- IBM Associate Systems Engineer Job: Boost Your Career with a Leading Technology Giant – codewindow.in
- Start Your IT Career Journey with Amazon: IT Services Support Associate I Opportunity – codewindow.in
- Shape the Future of Web: Front-End Software Engineer Opportunity at Google Cloud – codewindow.in
- Exciting Opportunity: Java Spring Boot Senior Developer Role at Infosys – codewindow.in
- Unlock Your Potential at Nokia: Software Engineer Opportunities Await – codewindow.in
- Join Microsoft’s World-Class Team as a Software Engineer and Shape the Future of Technology – codewindow.in
- Virtusa is Seeking Talented React JS Developers to Drive Digital Excellence – codewindow.in
- Join IBM Dynamic Team as a Full Stack Developer and Shape the Future – codewindow.in
- EY Welcomes Aspiring AI/ML Interns to Unlock the Future of -codewindow.in
- Wipro is Hiring Test Engineers to Elevate Quality Assurance – codewindow.in
- Deloitte Is Hiring: Analysts and Senior Analysts Wanted to Drive Innovation – codewindow.in
- Exciting Software Development Opportunity At Oracle – codewindow.in
- PayPal Hiring Data Analyst 1 – codewindow.in
- SIEMENS HEALTHINEERS Hiring Data Scientist ( ML & AI) – codewindow.in
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in
- Unstop Hiring Challenges Internships and Hackathons – codewindow.in
- Programming in Python – codewindow.in
- Programming in Python – codewindow.in
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in
- Data Structure – comprehensive guide – codewindow.in
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in
- Automata Fixing – codewindow.in
- Codewindow Mock Test 1
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in
- Pseudocode- codewindow.in
- Programming in C++ – codewindow.in
- Aptitude tricks – codewindow.in
- Aptitude Tricks – codewindow.in
- Test series – codewindow.in
- Aptitude Tricks – codewindow.in
- C++ Technical Questions – codewindow.in
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in
- March 2025 (1)
- January 2024 (2)
- October 2023 (14)
- September 2023 (4)
- August 2023 (26)
- July 2023 (69)
- June 2023 (40)
- May 2023 (53)
- April 2023 (67)
- March 2023 (189)
- February 2023 (102)
- January 2023 (40)
- November 2022 (7)
- October 2022 (13)
- September 2022 (7)
- August 2022 (24)
- July 2022 (16)
- June 2022 (33)
- May 2022 (32)
- April 2022 (20)
- March 2022 (11)
- February 2022 (24)
- January 2022 (21)
- December 2021 (10)
- November 2021 (81)
- October 2021 (42)
- September 2021 (44)
- August 2021 (10)
- July 2021 (9)
- June 2021 (68)
- May 2021 (47)
- April 2021 (129)
- March 2021 (116)
- January 2021 (3)
- December 2020 (2)
- November 2020 (8)
- October 2020 (34)
- September 2020 (15)
- April 2020 (2)