Prime Sum Codechef Solution | C++, Java, Python | APRIL CHALLENGE


Prime Sum Codechef Solution

Given two integers AA and BB.

  • Let PP denote a sequence of NN prime numbers such that the sum of the sequence is AA.
  • Let QQ denote a sequence of MM prime numbers such that the sum of the sequence is BB.

Let XX denote the maximum absolute difference between PiPi and QjQj among all valid pairs (i,j)(i,j) such that (1≤i≤N)(1≤i≤N) and (1≤j≤M)(1≤j≤M). Find the minimum possible value of XX over all possible sequences PP and QQ.

More formally, for all possible sequences PP and QQ, find the minimum value of max(|Pi−Qj|)max(|Pi−Qj|), where (1≤i≤N)(1≤i≤N) and (1≤j≤M)(1≤j≤M).

Print −1−1 if any one of the sequences cannot be formed.

Note that, |X||X| denotes the absolute value of a number XX. For example, |−4|=4|−4|=4 and |7|=7|7|=7.

Input Format

  • First line will contain TT, the number of test cases. Then the test cases follow.
  • Each test case contains two integers AA and BB.

Output Format

For each test case, find the minimum possible value of the maximum absolute difference between PiPi and QjQj for (1≤i≤N)(1≤i≤N) and (1≤j≤M)(1≤j≤M). If any of the sequences can not be formed, print −1−1 instead.


  • 1≤T≤1051≤T≤105
  • 1≤A≤10181≤A≤1018
  • 1≤B≤10181≤B≤1018

Sample Input 1 

3 6
3 2

Sample Output 1 



Test case 11: Let P={3}P={3} and Q={3,3}Q={3,3}.
The maximum absolute difference is 00. It can be shown that the maximum absolute difference can not be less than 00.

Test case 22: The only possible sequences are P={3}P={3} and Q={2}Q={2}.
The maximum absolute difference is 11. It can be shown that the maximum absolute difference can not be less than 11.

Prime Sum Codechef  SOLUTION

Download Code
login to google play console, youtube video search by image, how to add a google account, which are the google apps, what is the google app, how to add a new google account, all the google apps, which service is the part of google, adsetting google, google data studio, google DNS, GCP console, bigquery, google studio, google api console, google app engine, anthos, google apis, google public DNS, google audio, google launcher, gcloud console, google compute engine, google webmaster console, google logged-in devices, gdrive unlimited,google console tools,google studio login,my google voice,hey google service,find similar images google, my google forms, www google com bd search, search using images in google,google image search scan,google sea console,website console google, google sound, gdrive storage for students, google site console, google console login, webmaster console, searc h console, google server down, google backup login, photo finder google, google products and services, google console tool, google image check, google cloud search is used for, google dashboard studio, google voice and audio activity,gcp dashboards, google services down, google go app, google dns down, google image identifier,check picture on google,google new launcher,google image finder online,image check google google google go, image finder on google, finding images on google, photo find google, find a picture in google, find the photo on google, picture find google, image find on google, find with photo google, image finder google online, google find with image, photo finder in google, google find image search, search using image in google, search on google with image, google server down today, google server down news

Read More Post Here

Leave a Comment

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