Company: Qualcomm, Juniper Networks, Cisco, Adobe, Brocade
Question:
Example:
Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Solution {
public static void main (String[] args) {
Scanner sc = new Scanner (System.in);
int m = sc.nextInt();
for(int mi=0;mi<m;mi++){
int n = sc.nextInt();
String a = Integer.toBinaryString(n);
// System.out.println(a);
//String ar= new String[a.length()];
int count=0;
for(int i=0;i<a.length();i++){
if(a.charAt(i)=='1'){count++;}
}
System.out.println(count);
}
}
}
Execution Time: 0.09
Question:
Given a positive integer N, print count of set bits in it. For example, if the given number is 6, output should be 2 as there are two set bits in it.
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. The next T lines will contain an integer N.
Output:
Corresponding to each test case, in a new line, print count of set bits in it.
Constraints:
Output:
Corresponding to each test case, in a new line, print count of set bits in it.
Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 1000000
Example:
Input:
2
6
11
6
11
Output:
2
32
Code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Solution {
public static void main (String[] args) {
Scanner sc = new Scanner (System.in);
int m = sc.nextInt();
for(int mi=0;mi<m;mi++){
int n = sc.nextInt();
String a = Integer.toBinaryString(n);
// System.out.println(a);
//String ar= new String[a.length()];
int count=0;
for(int i=0;i<a.length();i++){
if(a.charAt(i)=='1'){count++;}
}
System.out.println(count);
}
}
}
Execution Time: 0.09
Comments
Post a Comment