Algorithms
Easy
2859
3093
Sum of Values at Indices With K Set Bits
sum-of-values-at-indices-with-k-set-bits
"""
You are given a 0-indexed integer array nums and an integer k.

Return an integer that denotes the sum of elements in nums whose corresponding indices have exactly k set 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 21 is 10101, which has 3 set bits.

Example 1:

Input: nums = [5,10,1,5,2], k = 1
Output: 13
Explanation: The binary representation of the indices are:
0 = 0002
1 = 0012
2 = 0102
3 = 0112
4 = 1002
Indices 1, 2, and 4 have k = 1 set bits in their binary representation.
Hence, the answer is nums[1] + nums[2] + nums[4] = 13.

Example 2:

Input: nums = [4,3,2,1], k = 2
Output: 1
Explanation: The binary representation of the indices are:
0 = 002
1 = 012
2 = 102
3 = 112
Only index 3 has k = 2 set bits in its binary representation.
Hence, the answer is nums[3] = 1.


Constraints:

 * 1 <= nums.length <= 1000
 * 1 <= nums[i] <= 105
 * 0 <= k <= 10
"""
class Solution:
    def sumIndicesWithKSetBits(self, nums: List[int], k: int) -> int:
        
You are given a 0-indexed integer array nums and an integer k.

Return an integer that denotes the sum of elements in nums whose corresponding indices have exactly k set 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 21 is 10101, which has 3 set bits.

Example 1:

Input: nums = [5,10,1,5,2], k = 1
Output: 13
Explanation: The binary representation of the indices are:
0 = 0002
1 = 0012
2 = 0102
3 = 0112
4 = 1002
Indices 1, 2, and 4 have k = 1 set bits in their binary representation.
Hence, the answer is nums[1] + nums[2] + nums[4] = 13.

Example 2:

Input: nums = [4,3,2,1], k = 2
Output: 1
Explanation: The binary representation of the indices are:
0 = 002
1 = 012
2 = 102
3 = 112
Only index 3 has k = 2 set bits in its binary representation.
Hence, the answer is nums[3] = 1.


Constraints:

 * 1 <= nums.length <= 1000
 * 1 <= nums[i] <= 105
 * 0 <= k <= 10


Please complete the code below to solve above prblem:
```python
class Solution:
    def sumIndicesWithKSetBits(self, nums: List[int], k: int) -> int:
        
```

my_solution = Solution()

test_input = { "nums": [5,10,1,5,2], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 13

test_input = { "nums": [4,3,2,1], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 1

test_input = { "nums": [1], "k": 0 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 1

test_input = { "nums": [100000], "k": 0 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 100000

test_input = { "nums": [2,2], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 2

test_input = { "nums": [2,4], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 4

test_input = { "nums": [2,7], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 7

test_input = { "nums": [3,3], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 3

test_input = { "nums": [3,9], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 9

test_input = { "nums": [4,7], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 7

test_input = { "nums": [4,8], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 8

test_input = { "nums": [6,6], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 6

test_input = { "nums": [7,2], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 2

test_input = { "nums": [7,4], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 4

test_input = { "nums": [8,4], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 4

test_input = { "nums": [8,9], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 9

test_input = { "nums": [9,9], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 9

test_input = { "nums": [15,43], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 43

test_input = { "nums": [35,86], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 86

test_input = { "nums": [36,14], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 14

test_input = { "nums": [47,61], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 61

test_input = { "nums": [60,46], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 46

test_input = { "nums": [70,7], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 7

test_input = { "nums": [1,51,55], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 106

test_input = { "nums": [2,2,5], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 7

test_input = { "nums": [2,3,2], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 5

test_input = { "nums": [3,2,5], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 7

test_input = { "nums": [3,7,5], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 12

test_input = { "nums": [4,5,9], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 14

test_input = { "nums": [5,5,5], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 10

test_input = { "nums": [5,7,7], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 14

test_input = { "nums": [6,2,1], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 3

test_input = { "nums": [6,9,8], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 17

test_input = { "nums": [7,1,2], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 3

test_input = { "nums": [7,9,1], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 10

test_input = { "nums": [8,5,4], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 9

test_input = { "nums": [9,1,6], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 7

test_input = { "nums": [9,3,5], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 8

test_input = { "nums": [57,48,69], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 117

test_input = { "nums": [78,37,59], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 96

test_input = { "nums": [96,71,53], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 124

test_input = { "nums": [900,914,367], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 1281

test_input = { "nums": [1,4,9,2], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 2

test_input = { "nums": [1,5,9,5], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 5

test_input = { "nums": [1,8,5,6], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 13

test_input = { "nums": [2,2,1,2], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 3

test_input = { "nums": [2,4,5,2], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 2

test_input = { "nums": [2,5,8,1], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 13

test_input = { "nums": [2,7,3,9], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 10

test_input = { "nums": [3,5,4,2], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 2

test_input = { "nums": [4,1,6,3], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 3

test_input = { "nums": [6,3,8,8], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 8

test_input = { "nums": [6,6,1,4], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 4

test_input = { "nums": [7,1,9,6], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 6

test_input = { "nums": [7,5,2,1], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 7

test_input = { "nums": [7,5,3,4], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 8

test_input = { "nums": [7,8,6,2], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 14

test_input = { "nums": [8,3,9,8], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 8

test_input = { "nums": [8,7,3,2], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 2

test_input = { "nums": [9,4,2,2], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 6

test_input = { "nums": [9,6,8,8], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 14

test_input = { "nums": [9,7,8,9], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 9

test_input = { "nums": [9,40,73,19], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 19

test_input = { "nums": [41,51,58,2], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 109

test_input = { "nums": [44,96,36,56], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 56

test_input = { "nums": [1,1,3,1,6], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 1

test_input = { "nums": [2,1,1,3,5], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 7

test_input = { "nums": [2,5,4,3,1], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 3

test_input = { "nums": [3,2,7,1,5], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 14

test_input = { "nums": [4,2,8,8,2], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 8

test_input = { "nums": [4,3,7,8,8], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 8

test_input = { "nums": [4,6,2,2,7], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 15

test_input = { "nums": [4,7,5,1,1], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 1

test_input = { "nums": [5,6,6,6,3], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 15

test_input = { "nums": [6,4,8,4,2], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 4

test_input = { "nums": [7,7,9,5,8], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 24

test_input = { "nums": [7,9,1,3,2], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 3

test_input = { "nums": [8,5,6,9,7], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 9

test_input = { "nums": [8,6,9,4,4], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 4

test_input = { "nums": [8,8,2,9,2], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 12

test_input = { "nums": [9,3,7,9,6], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 16

test_input = { "nums": [9,5,5,5,5], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 15

test_input = { "nums": [27,73,37,82,78], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 188

test_input = { "nums": [36,28,94,49,79], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 49

test_input = { "nums": [48,54,75,72,77], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 206

test_input = { "nums": [574,419,838,216,442], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 1699

test_input = { "nums": [1,1,1,2,5,7], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 9

test_input = { "nums": [2,6,6,8,6,4], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 12

test_input = { "nums": [2,8,2,9,2,8], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 17

test_input = { "nums": [2,9,1,6,5,7], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 13

test_input = { "nums": [2,9,1,6,6,7], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 13

test_input = { "nums": [3,5,4,5,8,9], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 17

test_input = { "nums": [5,5,3,7,9,7], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 17

test_input = { "nums": [5,9,4,8,7,2], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 10

test_input = { "nums": [5,9,6,6,4,5], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 11

test_input = { "nums": [6,4,7,8,4,7], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 15

test_input = { "nums": [6,8,6,2,7,3], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 5

test_input = { "nums": [7,2,5,4,4,4], "k": 1 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 11

test_input = { "nums": [7,2,9,7,8,7], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 14

test_input = { "nums": [7,6,6,7,6,1], "k": 2 }
assert my_solution.sumIndicesWithKSetBits(**test_input) == 8