forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sum-of-values-at-indices-with-k-set-bits.cpp
41 lines (38 loc) · 1.14 KB
/
sum-of-values-at-indices-with-k-set-bits.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Time: O(C(ceil(log2(n)), k))
// Space: O(1)
// bit manipulation, hakmem-175
class Solution {
public:
int sumIndicesWithKSetBits(vector<int>& nums, int k) {
const auto& next_popcount = [](uint32_t n) { // reference: https://massivealgorithms.blogspot.com/2014/06/hakmem-item-175.html
const uint32_t lowest_bit = n & -n;
const uint32_t left_bits = n + lowest_bit;
const uint32_t changed_bits = n ^ left_bits;
const uint32_t right_bits = (changed_bits / lowest_bit) >> 2;
return left_bits | right_bits;
};
int result = 0;
for (int i = (1 << k) - 1; i < size(nums); i = next_popcount(i)) {
result += nums[i];
if (i == 0) {
break;
}
}
return result;
}
};
// Time: O(n)
// Space: O(1)
// bit manipulation
class Solution2 {
public:
int sumIndicesWithKSetBits(vector<int>& nums, int k) {
int result = 0;
for (int i = 0; i < size(nums); ++i) {
if (__builtin_popcount(i) == k) {
result += nums[i];
}
}
return result;
}
};