Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Sunday, February 26, 2017

A Game of 2 geeks solution


#include<stdio.h>
int main()
{
unsigned int T;
         scanf("%u",&T);
         while(T--){
             unsigned long long n,temp,c=0;
             scanf("%llu",&n);
             while(n!=1){
                if(!(n&(n-1))){
                    c++;
                    n=n>>1;
                }
                else{
                    temp=~0;// temp = ~0= 111111......upto 63 times
                    while(temp>=n)
                       temp=temp>>1;
                    n=(n-(temp+1));
                    c++;
                }
             }
             if(c&1)
              printf("Karan\n");
             else
              printf("Arjun\n");
         }
return 0;
}





Code 2 :


    string name[]={"Arjun","Karan"};
    cin>>n;
    int count=0;
    while(n>1)
    {
    if((n&(n-1))==0)
    n=n>>1;
    else
    {
    string str=bitset<64>(n).to_string();
    int i=0;
    while(str[i]!='1')
    i++;
    //cout<<"n="<<n<<"  "<<((unsigned long long)1<<(63-i))<<"\n";
    n=n-((unsigned long long)1<<(63-i));
    /*if((n&(n-1))==0)
       n=n>>1;*/
}
count++;
//cout<<"n="<<n<<"\n";
}
cout<<name[count%2]<<"\n";
    }

return 0;



code 3"


#include<iostream>
#include<bitset>
using namespace std;
int main()
 {
int t;
cin>>t;
while(t--)
    {
    unsigned long long n;
    string name[]={"Arjun","Karan"};
    cin>>n;
    int count=0;
    while(n>1)
    {
    if((n&(n-1))!=0)
    {
    unsigned long long p=n;
    while((p&(p-1))!=0)
    {
    p=(p&(p-1));
}
n-=p;
    }
    else
    {
    n=n>>1;
}
count++;
}
cout<<name[count%2]<<"\n";
    }

return 0;
}

0 Comments:

Post a Comment

Stats