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

Powered by Blogger.

Saturday, February 25, 2017

Count no of set bits


import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
 {
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t>0)
{
   int count=0;
   int n=ab.nextInt();
   while(n>0)
   {
       n&=(n-1);
       count++;
   }
   System.out.println(count);
   t--;
}
}
}

0 Comments:

Post a Comment

Stats