Monk's Choice Of Numbers-Hackerearth


JAVA Solution


import java.util.*;
class TestClass {
    public static void main(String args[] ) throws Exception {
        Scanner s = new Scanner(System.in);
        int t = s.nextInt();
        while(t!=0)
        {
            ArrayList<Integer>hs = new ArrayList<Integer>();
            int n = s.nextInt(), k = s.nextInt(), i, count=0;
            int ar[] = new int[n];
            for(i=0;i<n;i++)
            {
                ar[i] = s.nextInt();
                String str = Integer.toBinaryString(ar[i]);
                str = str.replaceAll("0","");
                hs.add(str.length());
            }
            int len = hs.size();
            Integer ak[] = new Integer[len];
            hs.toArray(ak);
            Arrays.sort(ak);
            i=0;len--;
            while(i<k)
            {
                count+=ak[len];
                i++;
                len--;
            }
            System.out.println(count);
            t--;
        }
    }
}


Try this on Hackerearth

Click this link for Hackerearth Problems

Comments

Popular posts from this blog

First Unique Character in a String

Balanced Binary Tree

Majority Element

Sequential Digits