Monk has a very good friend, Puchi. As weird as his name, are the games he plays.
One fine day, they decided to play a game to test how diverse their choices are. Both of them choose exactly one integer each. Monk chooses an integer M and Puchi chooses an integer P.
The diversity of their choices is defined as the number of bits whose status is different in the binary representation of M and P, i.e., count of bits that are, either set in M and unset in P or set in P and unset in M.
Find the answer to their game.
Example:
Input: a = 1, b = 4
Output: 2
Approach
C++
#include <bits/stdc++.h>using namespace std;long long monkAndFriend(long long a, long long b){long long res = 0;while (a || b){if (a % 2 == 0 && b % 2 != 0)res++;else if (a % 2 != 0 && b % 2 == 0)res++;a = a / 2;b = b / 2;}return res;}int main(){long long a = 1, b = 4;long long ans = monkAndFriend(a, b);cout << ans << "\n";return 0;}
No comments:
Post a Comment