NR2 - Bhagat The Bit Man - SPOJ Solution C++

  Problem Link : NR2 


👉 Hint : Use bitwise operators like shift

 


✅ C++ Solution :

 
#include<bits/stdc++.h>
using namespace std;
 
#define ll long long int
 
int main()
{
    ll n;
    cin>>n;
    ll arr[n];
    for(ll i=0;i<n;i++)
        cin>>arr[i];
    bool one,zero;
    ll curr=0;
    for(ll i=59;i>=0;i--)
    {
        one=0,zero=0;
        for(ll j=0;j<n;j++)
        {
 
            if(arr[j] >= (ll)1<<i && (arr[j] & (ll)1<<i)!=0)
                one=1;
            else
                zero=1;
            if(one && zero)
                break;
        }
        if(one && zero)
            curr=curr*2+1;
        else
            curr=curr*2;
    }
    cout<<curr;
}

 

Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook. 

😇Happy Learning!!