![]() |
VOOZH | about |
Given two numbers, the task is to use alternative bits within two numbers to create result. We take first bits of second number, then second bit of the first number, third bit of second number and take the fourth bit of a first number and so on and generate a number with it.
Examples :
Input : n = 10, m = 11 Output : 11 Start from right of second number Binary representation of n = 1 0 1 0 ^ ^ Binary representation of m = 1 0 1 1 ^ ^ Output is = 1 0 1 1 Input : n = 20, m = 7 Output : 5 Start from right of second number binary representation of n = 1 0 1 0 0 ^ ^ binary representation of m = 0 0 1 1 1 ^ ^ ^ Output is = 0 0 1 0 1
Approach :-
1. Get the set even bits number of n.
2. Get the set odd bits number of m.
3. return OR of these number.
Output :
11
Time complexity: O(logn + logm)
Auxiliary Space: O(1)