Class Solution
-
- All Implemented Interfaces:
public final class Solution2859 - Sum of Values at Indices With K Set Bits\.
Easy
You are given a 0-indexed integer array
numsand an integerk.Return an integer that denotes the sum of elements in
numswhose corresponding indices have exactlykset 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
21is10101, which has3set bits.
Example 1:
Input: nums = 5,10,1,5,2, k = 1
Output: 13
Explanation: The binary representation of the indices are:
0 = 000<sub>2</sub>
1 = 001<sub>2</sub>
2 = 010<sub>2</sub>
3 = 011<sub>2</sub>
4 = 100<sub>2</sub>
Indices 1, 2, and 4 have k = 1 set bits in their binary representation. Hence, the answer is nums1 + nums2 + nums4 = 13.
Example 2:
Input: nums = 4,3,2,1, k = 2
Output: 1
Explanation: The binary representation of the indices are:
0 = 00<sub>2</sub>
1 = 01<sub>2</sub>
2 = 10<sub>2</sub>
3 = 11<sub>2</sub>
Only index 3 has k = 2 set bits in its binary representation. Hence, the answer is nums3 = 1.
Constraints:
1 <= nums.length <= 1000<code>1 <= numsi<= 10<sup>5</sup></code>
0 <= k <= 10
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public classSolution.Companion
-
Field Summary
Fields Modifier and Type Field Description public final static Solution.CompanionCompanion
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final IntegersumIndicesWithKSetBits(List<Integer> nums, Integer k)-
-
Method Detail
-
sumIndicesWithKSetBits
final Integer sumIndicesWithKSetBits(List<Integer> nums, Integer k)
-
-
-
-