Header Ads Widget

Find an integer | codechef solution

Problem

Chef has two nephews who love integers  and . An integer  is awesome if the following conditions hold:

  • 11018
  • (+) is divisible by .
  • (+) is divisible by .

Find any awesome integer . We can prove that under the given constraints, an answer always exists.

Note that you do not have to minimize the answer.

Input Format

  • The first line of input will contain a single integer , denoting the number of test cases.
  • Each test case consists of a single line of input.
    • The first line of each test case contains two space-separated integers  and  — favourite numbers of Chef's nephews.

Output Format

For each test case, output an awesome integer. If there are several answers, you may print any of them.

Constraints

  • 1105
  • 1,109

Sample 1:

Input
Output
3
18 42
1 1
100 200
192
5
500

Explanation:

Test Case 1: In this case, =192 is an awesome integer because:

  • 11921018
  • 192+18=210 is divisible by 42
  • 192+42=234 is divisible by 18

Test Case 2: We can output any positive integer  such that (11018) because +1 is divisible by 1 for all such .

 Code(C++):-

#include <iostream>
#include <numeric>
using namespace std;

int main() {
long long int t,i,a,b,c,d;
cin>>t;
for(i=0;i<t;i++){
cin>>a>>b;
c=gcd(a,b);
if(c!=min(a,b)){
d=(a/c)-1;
cout<<b*d-a<<endl;
}
else if(a!=b){
cout<<(max(a,b))-(min(a,b))<<endl;
}
else cout<<a+b<<endl;
}
    return 0;
}

Code(JAVA):-
import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        
            Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        while (T-->0) {
long a = sc.nextLong();
long b = sc.nextLong();
boolean printed = false;
if (a != b) {
long N = a*b - a-b;
if (N<0) {
N = 2*a*b-a-b;
}
System.out.println(N);
printed = true;
}
if (printed == false) {
System.out.println(a);
}
        }
    }
}

Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-

Companies interview:-

Full C course:-    

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-