2859. Sum of Values at Indices With K Set Bits Easy
1/**
2 * [2859] Sum of Values at Indices With K Set Bits
3 *
4 * You are given a 0-indexed integer array nums and an integer k.
5 * Return an integer that denotes the sum of elements in nums whose corresponding indices have exactly k set bits in their binary representation.
6 * The set bits in an integer are the 1's present when it is written in binary.
7 *
8 * For example, the binary representation of 21 is 10101, which has 3 set bits.
9 *
10 *
11 * <strong class="example">Example 1:
12 *
13 * Input: nums = [5,10,1,5,2], k = 1
14 * Output: 13
15 * Explanation: The binary representation of the indices are:
16 * 0 = 0002
17 * 1 = 0012
18 * 2 = 0102
19 * 3 = 0112
20 * 4 = 1002
21 * Indices 1, 2, and 4 have k = 1 set bits in their binary representation.
22 * Hence, the answer is nums[1] + nums[2] + nums[4] = 13.
23 * <strong class="example">Example 2:
24 *
25 * Input: nums = [4,3,2,1], k = 2
26 * Output: 1
27 * Explanation: The binary representation of the indices are:
28 * 0 = 002
29 * 1 = 012
30 * 2 = 102
31 * 3 = 112
32 * Only index 3 has k = 2 set bits in its binary representation.
33 * Hence, the answer is nums[3] = 1.
34 *
35 *
36 * Constraints:
37 *
38 * 1 <= nums.length <= 1000
39 * 1 <= nums[i] <= 10^5
40 * 0 <= k <= 10
41 *
42 */
43pub struct Solution {}
44
45// problem: https://leetcode.com/problems/sum-of-values-at-indices-with-k-set-bits/
46// discuss: https://leetcode.com/problems/sum-of-values-at-indices-with-k-set-bits/discuss/?currentPage=1&orderBy=most_votes&query=
47
48// submission codes start here
49
50impl Solution {
51 pub fn sum_indices_with_k_set_bits(nums: Vec<i32>, k: i32) -> i32 {
52 0
53 }
54}
55
56// submission codes end
57
58#[cfg(test)]
59mod tests {
60 use super::*;
61
62 #[test]
63 fn test_2859() {
64 }
65}
66
Back
© 2025 bowen.ge All Rights Reserved.