{"id":4656,"date":"2019-01-17T02:11:44","date_gmt":"2019-01-17T10:11:44","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4656"},"modified":"2019-01-17T02:21:20","modified_gmt":"2019-01-17T10:21:20","slug":"leetcode-974-subarray-sums-divisible-by-k","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-974-subarray-sums-divisible-by-k\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 974. Subarray Sums Divisible by K"},"content":{"rendered":"\n<p>Given an array&nbsp;<code>A<\/code>&nbsp;of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by&nbsp;<code>K<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>A = [4,5,0,-2,-3,1], K = 5 <br><strong>Output: <\/strong>7 <br><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>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>1 &lt;= A.length &lt;= 30000<\/code><\/li><li><code>-10000 &lt;= A[i] &lt;= 10000<\/code><\/li><li><code>2 &lt;= K &lt;= 10000<\/code><\/li><\/ol>\n\n\n\n<p><strong>Solution:\u00a0Count prefix\u00a0sums<\/strong><\/p>\n\n\n\n<p>let c[i] denotes the counts of prefix_sum % K init: c[0] = 1 <br>Whenever we end up with the same prefix sum (after modulo), which means there are subarrys end with current element that is divisible by K (0 modulo).<\/p>\n\n\n\n<p>e.g. A = [4,5,0,-2,-3,1], K = 5<br>[4,5] has prefix sum of 4, which happens at index 0 [4], and index 1, [4,5]<br>[4,5,0] also has a prefix sum of 4, which means [4, <strong>{5,0}<\/strong>], [4,5, {<strong>0}<\/strong>] are divisible by K.<br><br>ans += (c[prefix_sum] &#8211; 1)<br>i = 0, prefix_sum = 0, c[(0+4)%5] = c[4] = 1, ans = 0<br>i = 1, prefix_sum = 4+5, c[(4+5)%5] = c[4] = 2, ans = 0+2-1=0 => [5]<br>i = 2, prefix_sum = 4+0, c[(4+0)%5] = c[4] = 3, ans = 1+3-1=3 => [5], [5,0], [0]<br>i = 3, prefix_sum = 4-2, c[(4-2)%5] = c[2] = 1, ans = 3<br>i = 4, prefix_sum = 2-3, c[(2-3+5)%5] = c[4] = 4, ans = 3+4-1=6 => [5],[5,0],[0],[5,0,-2,-3], [0,-2,-3],[-2,-3]<br>i = 5, prefix_sum = 4+1, c[(4+1)%5] = c[0] = 2, ans = 6 + 2 &#8211; 1 =>  <br>[5],[5,0],[0],[5,0,-2,-3], [0,-2,-3],[-2,-3], [4,5,0,-2,-3,1]<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  int subarraysDivByK(vector<int>& A, int K) {    \n    vector<int> c(K);\n    c[0] = 1;\n    int ans = 0;\n    int sum = 0;\n    for (int a : A) {\n      sum = (sum + a % K + K) % K;\n      ans += c[sum]++;\n    }\n    return ans;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\nclass Solution:\n  def subarraysDivByK(self, A, K):\n    c = [0] * K\n    c[0] = 1\n    s = 0\n    ans = 0    \n    for a in A:\n      s = (s + a % K + K) % K\n      ans += c[s]\n      c[s] += 1\n    return ans\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array&nbsp;A&nbsp;of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by&nbsp;K. Example 1: Input: A = [4,5,0,-2,-3,1], K =&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[18,177,200,41],"class_list":["post-4656","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-medium","tag-prefix-sum","tag-subarray","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4656","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=4656"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4656\/revisions"}],"predecessor-version":[{"id":4661,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4656\/revisions\/4661"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4656"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4656"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4656"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}