Min operations

Tags : geeksforgeeks, math, cpp, easy

Given two numbers a and b. In one operation you can pick any non negative integer x and either of a or b. Now if you picked a then replace a with a&x else if you picked b then replace b with b&x.

Return the minimum number of operation required to make a and b equal.

Note: Here & represents bitwise AND operation.

Your Task:

You don’t need to read, input, or print anything. Your task is to complete the function solve( ), which takes two integers a and b as input parameters and returns the answer.

Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)

Examples #

Example 1:

Input:
a = 5, b = 12
Output:
2
Explanantion:
In first operation replace 
a = a&4 = 4
after that replace 
b = b&6 = 4
Hence both are same after applying two
operations.

Example 2:

Input: 
a = 100, b = 100
Output: 
0
Explanation: 
Already same.

Constraints #

Solutions #


class Solution{
  public:
    int solve(int a, int b) {
        // code here
        int x = a&b;
        int res = 0;
        if(x != a) {
            res++;
        } 
        if(x != b) {
            res++;
        }
        return res;
    }
};