Meta Data#
Difficulty: easy First Attempt: 2025-10-12
- Submissions: Accepted (first try)
Approach#
Count each value with a Counter (Hash Table Template 1). For a value whose count is divisible by k, every occurrence counts, so add value × count.
My Solution#
class Solution:
def sumDivisibleByK(self, nums: List[int], k: int) -> int:
return sum(x * c for x, c in Counter(nums).items() if c % k == 0)
Complexity#
Time O(n), space O(n).
Mistakes to Avoid#
- Add every occurrence, not the value once.
Interview Explanation#
I count each value, and when its count is divisible by k I add the value times its count. Time O(n).
