Ctrl K

Indices With K Ones

Easy
You are given an array values (0-indexed) and an integer ones. Write each index in binary and count its one bits. Return the sum of the elements whose index has exactly ones one bits in binary.
Example 1
values=[7, 2, 9, 4, 1, 6], ones=2 → 10

The indices in binary are 0, 1, 10, 11, 100, 101. Indices 3 (11) and 5 (101) have two ones: 4 + 6 = 10.

Example 2
values=[8, 3, 5], ones=0 → 8

Only index 0 has zero ones, so the answer is 8.

Constraints
  • 1 ≤ len(values) ≤ 1000
  • 1 ≤ values[i] ≤ 10⁵
  • 0 ≤ ones ≤ 10
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsThe Unpaired Value EasyThe Lost Ticket EasyDoubled Tag, Lost Tag EasyMirror and Negate a Bitmap EasyMultiple of Five at Every Step Easy
loading Python…
Input
values=[7, 2, 9, 4, 1, 6], ones=2
Expected
10

Run checks the first 3 tests in your browser. Submit checks all 72 tests on the server, hidden ones included.