Skip to content

Latest commit

 

History

History
36 lines (28 loc) · 724 Bytes

Question_2859.md

File metadata and controls

36 lines (28 loc) · 724 Bytes

LeetCode Records - Question 2859 Sum of Values at Indices With K Set Bits

Attempt 1: Ue a helper function to check the set bits

class Solution {
    public int sumIndicesWithKSetBits(List<Integer> nums, int k) {
        int sum = 0;

        int i = 0;
        for (int num : nums) {
            if (isTarget(i, k)) {
                sum += num;
            }

            i++;
        }

        return sum;
    }

    private boolean isTarget(int num, int k) {
        int count = 0;

        while (num > 0) {
            count += num & 1;
            num >>>= 1;
        }

        return count == k;
    }
}
  • Runtime: 2 ms (Beats: 38.46%)
  • Memory: 44.10 MB (Beats: 74.48%)