Minimum X (xor) A | Practice | GeeksforGeeks

Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.
Example 1:
Input: 
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.
Example 2:
Input: 
A = 7, B = 12
Output: 6
Explanation:
(7)2= 111
(12)2= 1100
The XOR will be minimum when x = 6 
i.e. (6 XOR 7) = 1 and the number 
of set bits in 6 is equal to the 
number of set bits in 12.
0
samikshamahure3 months ago
class Solution {
  public:
    int minVal(int a, int b) {
        //check the case if set bits(A)==set bits in(B)
        int A=a,B=b;
        int countA=0,countB=0;
        while(A){
            A=A&(A-1);
            countA++;
        }
        while(B){
            B=B&(B-1);
            countB++;
        }
        if(countA==countB) return a;
        int x=0;
        stack<short int>s;
        //push the binary of number in stack
        while(a){
           s.push(a%2);
           a=a/2;
        }
        while(!s.empty()){
             int rem=s.top();
            if(rem==1 && countB){
                //if it is set and count of set bit is less
                x=x<<1;
                x+=1;
                countB--;
            }
            else{
                x=x<<1;
            }
             s.pop();
        }
        if(!countB){
        return x;
        }
        else{
        //if number of bits is not equal to B 
            int i=1;
            while(countB){
                //traverse from lsb and add 1 if it is 0
                if((x & i)==0) {
                    x+=i;
                    countB--;
                    }
                else{
                    i++;
                }
        }
    return x;
        }
    }
};
0
kake13373 months ago
    int minVal(int a, int b) {
       int c=0;
       while(b>0)
       {
           b=b&(b-1);
           c++;
       }
       int x=0;
       int i=log2(a);
       while(c>0 && i>=0)
       {
           if(a&(1<<i))
           {
               c--;
               x=x|(1<<i);
           }
           i--;
       }
       i=0;
       while(c>0)
       {
           if(!(x&(1<<i)))
           {
               c--;
               x=x|(1<<i);
           }
           i++;
       }
       return x;
0
chessnoobdj4 months ago
Easy greedy c++
int minVal(int a, int b) {
        int cnt_a = __builtin_popcount(a);
        int cnt_b = __builtin_popcount(b);
        int num = 0;
        if(cnt_a >= cnt_b){
            num = a;
            int tmp = cnt_a-cnt_b, i=0;
            while(tmp){
                if((1&(num>>i)) == 1){
                    num ^= (1<<i);
                    tmp -= 1;
                }
                i += 1;
            }
        }
        else{
            num = a;
            int tmp = cnt_b-cnt_a, i=0;
            while(tmp){
                if((1&(num>>i)) == 0){
                    num |= 1<<i;
                    tmp -= 1;
                }
                i += 1;
            }
        }
        return num;
    }
0
Jyoti9 months ago
Jyoti
yeahhhhh.......first time i solved a hard level question without help. basically the idea is Traverse from the MSB in A to the LSB and if a bit is 1 then it also needs to be 1 in the answer in order to minimize the XOR but the number of bits set has to be equal to the number of set bits in B. So, when the count of set bits in the answer has reached the count of 1s in B then the rest of the bits have to be 0.but there will be some case where you will reached to LSB and count of 1s in answer is not equal to count of 1s in B, then start traversing from LSB to MSB and if the bit is 0 then make it 1 in the answer do this untill no of 1s in answer and B is equal.
     int getSetBits(int b)    {        int count=0;        while(b)        {            b = b & (b-1);            count++;        }        return count;    }    int minVal(int a, int b) {        // code here        int setBits = getSetBits(b);        //cout<<setbits<<"hello\n";<br>        int ans = 0;        for (int i = 31; i >= 0; i--) {            int mask = 1 << i;            bool set = a & mask;            if (set && setBits > 0) {                ans |= (1 << i);                setBits--;            }        }        //cout<<setbits<<"hi\n";<br>        if(setBits>0)        {            for(int i=0;i<32;i++)            {                int mask = 1 << i;                bool set = ans & mask;                if(!set && setBits)                {                    ans|=(1<<i); setbits--;="" }<br="">            }        }             return ans;    }
0
wallflower10 months ago
wallflower
Java easy solution. https://ide.geeksforgeeks.o...
0
Motasim1 year ago
Motasim
C++ 0.1 sec Solution:https://ide.geeksforgeeks.o...
0
SHIV RAM1 year ago
SHIV RAM
Hi @moderators... Please change the the question description to : " count of set bit in X is less than or equal to the count of set bits in B" or change the back-end code, Since it is producing wrong output for the cases when number of set bits in B is greater than number of set bits in A.
Since the problem is tagged as Hard, it would be apt to change the back-end function which handles all the cases correctly.
-1
Yash Khandelwal1 year ago
Yash Khandelwal
GFG is Known for its Incorrect Test cases!!.
0
Akshansh Jain1 year ago
Akshansh Jain
For input A = 9, B = 7, the answer of online judge is 9. According to question, number of set bits in X should be equal to number of set bits in B. I assume set bits are the number of 1's in the binary representation of the decimal. Here, X=9 (as given by online judge), and B = 7.
9's binary representation = 1001, number of set bits = 2.7's binary representation = 111, number of set bits = 3.
So, 9 should not even be a possible answer here, let alone the correct answer, as number of bits are not equal.
Please correct me if I have misunderstood anything.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.