<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the number of non-empty <strong>subarrays</strong> that have a sum divisible by </em><code>k</code>.</p>

<p>A <strong>subarray</strong> is a <strong>contiguous</strong> part of an array.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [4,5,0,-2,-3,1], k = 5
<strong>Output:</strong> 7
<strong>Explanation:</strong> There are 7 subarrays with a sum divisible by k = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [5], k = 9
<strong>Output:</strong> 0
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>4</sup></code></li>
	<li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
	<li><code>2 &lt;= k &lt;= 10<sup>4</sup></code></li>
</ul>

'''
there are n^2 possible subarrays, for each we could calculate sum and see if its divisible by k could be n^3 time

if our current sum

n % k = 0

n / k = has no remainder
n + 1 % k = 1
so if we search for our remainder/or some sum that has our current remainder too, and subtract that from our current sum, that is a valid a subarray dividisble by k
'''

class Solution:
    def subarraysDivByK(self, nums: List[int], k: int) -> int:
        sm = 0
        sums = {0 : 1}
        total = 0
        for num in nums:
            sm += num
            r = sm % k
            sm = r
            # print(r, total)
            if r in sums:
                total += sums[r]
            sums[r] = sums.get(r, 0) + 1
        return total
21200