Header Ad

HackerEarth Monk and his Friend problem solution

In this HackerEarth Monk and his Friend, problem-solution 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.


HackerEarth Monk and his Friend problem solution


HackerEarth Monk and his Friend's problem solution.

#include<bits/stdc++.h>


using namespace std;

#define rep(i,n) for(i=0;i<n;i++)
#define ll long long
#define elif else if
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define CLEAR(array, value) memset(ptr, value, sizeof(array));
#define si(a) scanf("%d", &a)
#define sl(a) scanf("%lld", &a)
#define pi(a) printf("%d", a)
#define pl(a) printf("%lld", a)
#define pn printf("\n")

ll int foo(ll int n)
{
ll int count = 0;
while(n)
{
count += n & 1;
n >>= 1;
}
return count;
}

int main()
{
freopen("in.txt","r",stdin);
freopen("out","w",stdout);
ios_base::sync_with_stdio(0);
int t;
cin>>t;
assert(1<=t && t<=10000);
while(t--)
{
ll int a,b;
cin>>a>>b;
assert(0<=a && a<=10000000000000000);
assert(0<=b && b<=10000000000000000);
a= a^b;
cout<<foo(a);
if(t>0)cout<<"\n";
}
return 0;
}

Second solution

#include <bits/stdc++.h>
using namespace std;

int main()
{
int T; cin >> T; assert(T>=1 && T<=10000);
for (int g=0; g<T; g++){
long long a, b; cin >> a >> b;
assert (a>=0 && a<=1e16);
assert (b>=0 && b<=1e16);
cout << __builtin_popcountll (a^b) << '\n';
}
return 0;
}

Post a Comment

0 Comments