Skip to main content
Medium Hash Map / Set Medium frequency

Subarray Sum Divisible by K

Open on LeetCode

Approach Summary

Prefix sum mod k. Two indices with the same running mod form a subarray divisible by k. Track mod frequencies in a HashMap.

How to Recognize This Pattern

  • "Subarrays divisible by k"
  • Prefix mod: same mod at two indices → divisible subarray between them

Complexity Analysis

Time Complexity

O(n)

Space Complexity

O(k)

Tags

Array Hash Table Prefix Sum

This site is free. If these guides are helping your prep, consider buying me a coffee. ☕

Support →
Buy me a coffee