Sum of Values at Indices With K Set Bits

You are given a 0-indexed integer array nums and an integer k.

Return an integer that denotes the sum of elements in nums whose corresponding indices have exactly k set bits in their binary representation.

The set bits in an integer are the 1's present when it is written in binary.

  • For example, the binary representation of 21 is 10101, which has 3 set bits.
Example 1
Inputnums = [5,10,1,5,2], k = 1
Output13
Indices 1, 2, and 4 have exactly k = 1 set bit in their binary representation, so the sum is nums[1] + nums[2] + nums[4] = 13.
Example 2
Inputnums = [4,3,2,1], k = 2
Output1
Only index 3 has exactly k = 2 set bits in its binary representation, so the sum is nums[3] = 1.

Constraints

  • 1 <= nums.length <= 1000
  • 1 <= nums[i] <= 10^5
  • 0 <= k <= 10

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate