SANHURRY-Editorial

PROBLEM LINK:

PRACTICE
Editorialist:Aditya Verma

DIFFICULTY:

EASY

PREREQUISITES:

Binary Representation

PROBLEM:

Santa has already distributed gifts to children in all other countries. Only the last two are left - country A and country B. As he proceeds to fly towards these neighboring countries in his magical sleigh, he finds out that Rudolph, his reindeer, in search for a snack, has mixed the last N gifts that Santa had so precisely distributed into two different sacks, one for the children in country A and the other for those in country B. Santa starts separating the gifts again, but he realizes he will be late and it will be dawn before he finishes distributing gifts to all the children. Help Santa in separating the gifts and be on time for all the children in these two countries. Each gift is marked with a number. If the binary representation of the number is a series of alternating digits, the gift goes into Sack A, else it goes into Sack B.

EXPLANATION:

We have to check for every number that its binary representation is alternating bits or not(101010…01).For ex: 5 has 101 binary representation, so it will goes to sack A otherwise sack B. We can get binary representation by cur=n%2 till n!=0 and if next n%2 is also same as cur then it is not alternating.

SOLUTION:

 #include <iostream>
using namespace std;
int main(void){
    
    int t,num,n,flag=0;
    cin>>t;
    while(t--)
    {
        cin>>num;
        int arr[num];
        for(int i=0;i<num;i++)
        {  
            flag=0;
            cin>>arr[i];
            n=arr[i];
            while(n>0)
            {
                int cur=n%2;
                n/=2;
                if(cur==n%2) 
                    flag=1;
            }
            if(flag==0)
                cout<<"Sack A"<<endl;
            else
                cout<<"Sack B"<<endl;
        }
        cout<<""<<endl;
    }
       
}
1 Like