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

Powered by Blogger.

Friday, August 25, 2017

Check set bits


Explanation:

operate &  with 1 , it will check whether the last bit is set or not.

Code:

import java.util.*;
import java.lang.*;
import java.io.*;
class hackerranksolutionc
 {
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t-->0)
{
   boolean flag=false;
   int n=ab.nextInt();
   while(n>1)
   {
       if((n&1)==0)
       {
           flag=true;
           break;
       }
       n=n>>1;
   }
   if(flag)
   System.out.println("NO");
   else
   System.out.println("YES");
}
}
}

0 Comments:

Post a Comment

Stats