You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<li>If there exist indices <span class="arithmatex">\(i\)</span> and <span class="arithmatex">\(j\)</span> such that <span class="arithmatex">\(i \leq j\)</span>, and the sum of the subarray <span class="arithmatex">\(nums[i, ..., j]\)</span> is divisible by <span class="arithmatex">\(k\)</span>, then <span class="arithmatex">\((s_j - s_i) \bmod k = 0\)</span>, this implies: <span class="arithmatex">\(s_j \bmod k = s_i \bmod k\)</span></li>
86196
-
<li>We can use a hash table to count the occurrences of prefix sums modulo <span class="arithmatex">\(k\)</span> to efficiently check for subarrays satisfying the condition.</li>
86197
-
</ul>
86198
-
</li>
86199
-
<li>
86200
-
<p><strong>Prefix Sum Modulo</strong>:</p>
86201
-
<ul>
86202
-
<li>Use a hash table <span class="arithmatex">\(cnt\)</span> to count occurrences of each prefix sum modulo <span class="arithmatex">\(k\)</span>.</li>
86203
-
<li><span class="arithmatex">\(cnt[i]\)</span> represents the number of prefix sums with modulo <span class="arithmatex">\(k\)</span> equal to <span class="arithmatex">\(i\)</span>.</li>
86204
-
<li>Initialize <span class="arithmatex">\(cnt[0] = 1\)</span> to account for subarrays directly divisible by <span class="arithmatex">\(k\)</span>.</li>
86205
-
</ul>
86206
-
</li>
86207
-
<li>
86208
-
<p><strong>Algorithm</strong>:</p>
86209
-
<ul>
86210
-
<li>Let a variable <span class="arithmatex">\(s\)</span> represent the running prefix sum, starting with <span class="arithmatex">\(s = 0\)</span>.</li>
86211
-
<li>Traverse the array <span class="arithmatex">\(nums\)</span> from left to right.<ul>
86212
-
<li>For each element <span class="arithmatex">\(x\)</span>:<ul>
86213
-
<li>Compute <span class="arithmatex">\(s = (s + x) \bmod k\)</span>.</li>
86214
-
<li>Update the result: <span class="arithmatex">\(ans += cnt[s]\)</span>.</li>
86215
-
<li>Increment <span class="arithmatex">\(cnt[s]\)</span> by <span class="arithmatex">\(1\)</span>.</li>
86216
-
</ul>
86217
-
</li>
86218
-
</ul>
86219
-
</li>
86220
-
<li>Return the result <span class="arithmatex">\(ans\)</span>.</li>
86221
-
</ul>
86222
-
</li>
86223
-
</ol>
86191
+
<p>Suppose there exists <span class="arithmatex">\(i \leq j\)</span> such that the sum of <span class="arithmatex">\(\textit{nums}[i,..j]\)</span> is divisible by <span class="arithmatex">\(k\)</span>. If we let <span class="arithmatex">\(s_i\)</span> represent the sum of <span class="arithmatex">\(\textit{nums}[0,..i]\)</span> and <span class="arithmatex">\(s_j\)</span> represent the sum of <span class="arithmatex">\(\textit{nums}[0,..j]\)</span>, then <span class="arithmatex">\(s_j - s_i\)</span> is divisible by <span class="arithmatex">\(k\)</span>, i.e., <span class="arithmatex">\((s_j - s_i) \bmod k = 0\)</span>, which means <span class="arithmatex">\(s_j \bmod k = s_i \bmod k\)</span>. Therefore, we can use a hash table to count the number of prefix sums modulo <span class="arithmatex">\(k\)</span>, allowing us to quickly determine if there exists a subarray that meets the condition.</p>
86192
+
<p>We use a hash table <span class="arithmatex">\(\textit{cnt}\)</span> to count the number of prefix sums modulo <span class="arithmatex">\(k\)</span>, where <span class="arithmatex">\(\textit{cnt}[i]\)</span> represents the number of prefix sums with a modulo <span class="arithmatex">\(k\)</span> value of <span class="arithmatex">\(i\)</span>. Initially, <span class="arithmatex">\(\textit{cnt}[0] = 1\)</span>. We use a variable <span class="arithmatex">\(s\)</span> to represent the prefix sum, initially <span class="arithmatex">\(s = 0\)</span>.</p>
86193
+
<p>Next, we traverse the array <span class="arithmatex">\(\textit{nums}\)</span> from left to right. For each element <span class="arithmatex">\(x\)</span>, we calculate <span class="arithmatex">\(s = (s + x) \bmod k\)</span>, then update the answer <span class="arithmatex">\(\textit{ans} = \textit{ans} + \textit{cnt}[s]\)</span>, where <span class="arithmatex">\(\textit{cnt}[s]\)</span> represents the number of prefix sums with a modulo <span class="arithmatex">\(k\)</span> value of <span class="arithmatex">\(s\)</span>. Finally, we increment the value of <span class="arithmatex">\(\textit{cnt}[s]\)</span> by <span class="arithmatex">\(1\)</span> and continue to the next element.</p>
86194
+
<p>In the end, we return the answer <span class="arithmatex">\(\textit{ans}\)</span>.</p>
86224
86195
<blockquote>
86225
-
<p>Note: if <span class="arithmatex">\(s\)</span> is negative, adjust it to be non-negative by adding <span class="arithmatex">\(k\)</span> and taking modulo <span class="arithmatex">\(k\)</span> again.</p>
86196
+
<p>Note: Since the value of <span class="arithmatex">\(s\)</span> can be negative, we can add <span class="arithmatex">\(k\)</span> to the result of <span class="arithmatex">\(s \bmod k\)</span> and then take modulo <span class="arithmatex">\(k\)</span> again to ensure that the value of <span class="arithmatex">\(s\)</span> is non-negative.</p>
86226
86197
</blockquote>
86227
-
<p>The time complexity is <span class="arithmatex">\(O(n)\)</span> and space complexity is <span class="arithmatex">\(O(n)\)</span> where <span class="arithmatex">\(n\)</span> is the length of the array <span class="arithmatex">\(nums\)</span>.</p>
86198
+
<p>The time complexity is <span class="arithmatex">\(O(n)\)</span>, and the space complexity is <span class="arithmatex">\(O(n)\)</span>. Here, <span class="arithmatex">\(n\)</span> is the length of the array <span class="arithmatex">\(\textit{nums}\)</span>.</p>
0 commit comments