Oniwa and Amix Numbers

Posted: 21 Apr, 2021
Difficulty: Moderate

PROBLEM STATEMENT

Try Problem

Nandy, Oniwa and Amix were having three integers ‘A’ ,’B’ ,’C’ .They want to a minimum number of Flip operation required in either ‘A’ or ‘B’ to make ‘A’ or ‘B’ == ‘C’ where ‘or’ is bitwise OR operation. The flip operation consists of changing a bit from 1 to 0 or from 0 to 1.

Nandy, Oniwa and Amix have to attend the meet of elite coders. So please find the minimum number of flips required to in A or B to make A or B equal to C.

Input Format:
The first line of input contains an integer ‘T’ denoting the number of test cases. 

The first and only line of the test case consists of three integers ‘A’, ‘B’, and ‘C’.
Output Format:
For each test case, print a single line containing a single integer denoting the minimum number of flips required.

The output of each test case will be printed in a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 11
1 <= A <= 10 ^ 9
1 <= B <= 10 ^ 9
1 <= C <= 10 ^ 9

Where 'T' is the number of test cases and 'A', 'B', 'C' are given integers.

Time Limit: 1 sec.
Approach 1

Run a loop from 0 to 32 since there are 32 bits in int . If the ith bit of C is 1 then at least ith bit of A or B should be 1. If none of the ith bit of A and B is 1 then set the bit of either A or B to 1.Thus one flip operation is required. If the ith bit of C is 0 then the ith bit of both A and B should be 0.  

 

Algorithm:

  • Run a loop from i equal 0 to 32.
  • Create a variable ans with default value 0.
  • If the ith bit of C is 1.Then check if ith of either A or B is one move to the next bit.Else set any of the ith bit of A or B to 1 and increase the ans by 1.
  • If the ith bit of C is 0 then the ith bit in both A and B should be 0. If the ith bit in A is 1 increase the ans by 1. Similarly, if the ith bit in B is 1 increase the ans by 1.
  • Return the ans.
Try Problem