Make A and B equal Codechef Solution

Hello Programmers In this post, you will know how to solve the Make A and B equal Codechef Solution. The Problem Code is EQUALISE.

Make A and B equal Codechef Solution
Make A and B equal Codechef Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Codechef by yourself. If you find any difficulty after trying several times, then you can look for solutions.

Problem

Chef has two numbers A and B.

In one operation, Chef can choose either A or B and multiply it by 2.

Determine whether he can make both A and B equal after any number (possibly, zero) of moves.

Input Format

  • The first line of input will contain a single integer T, denoting the number of test cases.
  • Each test case consists of two space-separated integers A and B.

Output Format

For each test case, output YES if Chef can make both numbers equal, NO otherwise.

Note that the checker is case-insensitive i.e. YESYesyesyES are all considered same.

Constraints

  • 1≤T≤2500
  • 1≤A,B≤50

Sample 1:

Input

4
5 20
6 6
12 2
50 20

Output

YES
YES
NO
NO

LinkedIn Skill Assessment Answers

Coursera Quiz Answers

Explanation:

Test case 1:  Chef can multiply A by 2 twice and both A and B will become 20.

Test case 2: Both numbers are already equal.

Test case 3: It can be shown that A and B  cannot be made equal.

Test case 4: It can be shown that A  and B cannot be made equal.

Make A and B equal Codechef Solutions in JAVA

/* package codechef; // don't place package name! */
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
{
    static boolean isPowerOfTwo(int n)
    {
        if (n == 0)
            return false;
        while (n != 1) {
            if (n % 2 != 0)
                return false;
            n = n / 2;
        }
        return true;
    }
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		int t;
		Scanner sc=new Scanner(System.in);
		t=sc.nextInt();
		while(t>0){
		    int a;
		    int b;
		    a=sc.nextInt();
		    b=sc.nextInt();
		    int k;
		    int count=0;
		    if(a%b==0){
		        count=1;
		    }else if(b%a==0){
		        count=1;
		    }
		     if(count==1){
		         if(a>b){
		            k=a/b;
		        }else{
		            k=b/a;
		        }
		    if(isPowerOfTwo(k)){
		        System.out.println("Yes");
		    }else{
		        System.out.println("No");
		    }
		   } else{
		       System.out.println("No");
		   }
		    t--;
		}
	}
}

Make A and B equal Codechef Solutions in CPP

#include<bits/stdc++.h>
// #pragma GCC optimize ("03")
// #pragma GCC optimize ("sse4")
#define ll   long long int
#define vi   vector<int>
#define uos	 unordered_set<int>
#define all(x)      	( x.begin() , x.end() )
#define sortvi(x)    	sort(x.begin() , x.end())
#define eb				emplace_back
#define nline 			"\n"
using namespace std;
/****************************************************************************************************/
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(NULL); cout.tie(0);
    int t ;
    cin>>t;
    while(t--){
        int a , b , c = 0 ;
        cin>>a>>b;
        int x = min(a , b) ;
        int y = max(a , b );
        while( x < y ){
            x*=2;
        }
        if( x == y ) cout<<"YES"<<nline;
        else cout<<"NO"<<nline;
    }
}

Make A and B equal Codechef Solutions in Python

# cook your dish here
for i in range(int(input())):
    a,b = map(int,input().split())
    if a > b :
        a,b=b,a
    while a < b:
        a = a*2
    print("YES" if a==b else 'NO')
Ezoicreport this ad

Disclaimer: The above Problem (Make A and B equal) is generated by CodeChef but the solution is provided by BrokenProgrammers. This tutorial is only for Educational and Learning purpose.

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Next: TV Discount Codechef Solution

Sharing Is Caring

Leave a Comment

Ezoicreport this ad