Skip to content

Commit

Permalink
counting number of bits
Browse files Browse the repository at this point in the history
  • Loading branch information
shanxS committed Aug 26, 2015
1 parent e3ccd9f commit d9867e8
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions java/counting number of bits.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// counting number of bits

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class Main
{
private static BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
private static Integer K, N;
private static List<Integer> numbers;

public static void main(String[] er)
{
try
{
Integer T = Integer.parseInt(bufferedReader.readLine());

for (int i=0; i<T; ++i)
{
String[] input = bufferedReader.readLine().split(" ");
N = Integer.parseInt(input[0]);
K = Integer.parseInt(input[1]);

numbers = new ArrayList<>();
TreeSet<Integer> counts = new TreeSet<>();
String[] inputs = bufferedReader.readLine().split(" ");
for(String s : inputs)
{
counts.add(countFor(Integer.parseInt(s)));
}

int count = 0;
while (K > 0)
{
count += counts.pollLast();
--K;
}
System.out.println(count);
}
}
catch (Exception e)
{
System.out.print("exception in buffered reader " + e.getMessage());
}

}

private static Integer countFor(Integer i)
{
Integer count = 0;

while (i > 0)
{
i = i&(i-1);
++count;
}

return count;
}
}

0 comments on commit d9867e8

Please sign in to comment.