Wednesday, June 8, 2022

Maximise Set Bits

 Let 

f(X) denote the number of set bits in X.

Given integers N and K, construct an array A of length N such that:

  • 0AiK for all (1iN);
  • i=1NAi=K

Over all such possible arrays, find the maximum value of i=1Nf(Ai).

Input Format

  • The first line of input contains a single integer T - the number of test cases. The description of T test cases follow.
  • The only line of each test case contains integers N and K- the length of the array and sum of elements respectively.

Output Format

For each test case, output the maximum value of i=1Nf(Ai).

Constraints

  • 1T104
  • 1N109
  • 0K109

Sample Input 1 

2
1 7
3 6

Sample Output 1 

3
4

Explanation

Test case 1: Only one array is possible that is A=[7]. As 7=(111)2, the number of set bits in 7 is 3. Thus, i=1Nf(Ai)=f(7)=3.

Test case 2: A possible array satisfying all the conditions is A=[3,0,3]. As 3=(11)2, the number of set bits in 3 is 2. Similarly, the number of set bits in 0 is 0. Thus, i=1Nf(Ai)=f(3)+f(0)+f(3)=4. It can be proven that answer never exceeds 4.

No comments:

Post a Comment

मुझे दिल में बसाना आसान नहीं....

पता है Recently i realised की मुझे दिल में बसाना आसान नहीं है बात-बात पे चिढ़ जाती हूं मैं दिल लगा लूं एक बार तो फिर ज़िद पे अड़...