2859. Sum of Values at Indices With K Set Bits

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public int sumIndicesWithKSetBits(List<Integer> nums, int k) {
int sum = 0;
for (int i = 0; i < nums.size(); i++) {
if (Integer.bitCount(i) == k) {
sum += nums.get(i);
}
}
return sum;
}
}

References

2859. Sum of Values at Indices With K Set Bits