{"id":7392,"date":"2020-09-20T21:25:59","date_gmt":"2020-09-21T04:25:59","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7392"},"modified":"2020-09-20T22:26:06","modified_gmt":"2020-09-21T05:26:06","slug":"leetcode-1590-make-sum-divisible-by-p","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1590-make-sum-divisible-by-p\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1590. Make Sum Divisible by P"},"content":{"rendered":"\n<p>Given an array of positive integers&nbsp;<code>nums<\/code>, remove the&nbsp;<strong>smallest<\/strong>&nbsp;subarray (possibly&nbsp;<strong>empty<\/strong>) such that the&nbsp;<strong>sum<\/strong>&nbsp;of the remaining elements is divisible by&nbsp;<code>p<\/code>. It is&nbsp;<strong>not<\/strong>&nbsp;allowed to remove the whole array.<\/p>\n\n\n\n<p>Return&nbsp;<em>the length of the smallest subarray that you need to remove, or&nbsp;<\/em><code>-1<\/code><em>&nbsp;if it&#8217;s impossible<\/em>.<\/p>\n\n\n\n<p>A&nbsp;<strong>subarray<\/strong>&nbsp;is defined as a contiguous block of elements in the array.<\/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> nums = [3,1,4,2], p = 6\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> The sum of the elements in nums is 10, which is not divisible by 6. We can remove the subarray [4], and the sum of the remaining elements is 6, which is divisible by 6.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [6,3,5,2], p = 9\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> We cannot remove a single element to get a sum divisible by 9. The best way is to remove the subarray [5,2], leaving us with [6,3] with sum 9.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,2,3], p = 3\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> Here the sum is 6. which is already divisible by 3. Thus we do not need to remove anything.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,2,3], p = 7\n<strong>Output:<\/strong> -1\n<strong>Explanation:<\/strong> There is no way to remove a subarray in order to get a sum divisible by 7.\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1000000000,1000000000,1000000000], p = 3\n<strong>Output:<\/strong> 0\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 10<sup>9<\/sup><\/code><\/li><li><code>1 &lt;= p &lt;= 10<sup>9<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: HashTable + Prefix Sum<\/strong><\/h2>\n\n\n\n<p>Very similar to subarray target sum.<\/p>\n\n\n\n<p>Basically, we are trying to find a shortest subarray that has sum % p equals to r = sum(arr) % p.<\/p>\n\n\n\n<p>We use a hashtable to store the <strong>last<\/strong> index of the prefix sum % p and check whether (prefix_sum + p &#8211; r) % p exists or not.<\/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 minSubarray(vector<int>& nums, int p) {\n    const int n = nums.size();\n    int r = accumulate(begin(nums), end(nums), 0LL) % p;\n    if (r == 0) return 0;\n    unordered_map<int, int> m{{0, -1}}; \/\/ {prefix_sum % p -> last_index}\n    int s = 0;\n    int ans = n;\n    for (int i = 0; i < n; ++i) {\n      s = (s + nums[i]) % p;\n      auto it = m.find((s + p - r) % p);\n      if (it != m.end())\n        ans = min(ans, i - it->second);\n      m[s] = i;\n    }\n    return ans == n ? -1 : 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 minSubarray(self, nums: List[int], p: int) -> int:    \n    r = sum(nums) % p\n    if r == 0: return 0\n    m = {0: -1}\n    ans = len(nums)\n    s = 0\n    for i, x in enumerate(nums):\n      s = (s + x) % p\n      t = (s + p - r) % p\n      if t in m:\n        ans = min(ans, i - m[t])\n      m[s] = i\n    return -1 if ans == len(nums) else ans\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of positive integers&nbsp;nums, remove the&nbsp;smallest&nbsp;subarray (possibly&nbsp;empty) such that the&nbsp;sum&nbsp;of the remaining elements is divisible by&nbsp;p. It is&nbsp;not&nbsp;allowed to remove the whole array.&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[82,177,200],"class_list":["post-7392","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-hashtable","tag-medium","tag-prefix-sum","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7392","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=7392"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7392\/revisions"}],"predecessor-version":[{"id":7395,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7392\/revisions\/7395"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7392"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7392"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}