{"id":6996,"date":"2020-06-28T00:50:56","date_gmt":"2020-06-28T07:50:56","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6996"},"modified":"2020-06-28T00:51:48","modified_gmt":"2020-06-28T07:51:48","slug":"leetcode-1497-check-if-array-pairs-are-divisible-by-k","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-1497-check-if-array-pairs-are-divisible-by-k\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1497. Check If Array Pairs Are Divisible by k"},"content":{"rendered":"\n<p>Given an array of integers&nbsp;<code>arr<\/code>&nbsp;of even length&nbsp;<code>n<\/code>&nbsp;and an integer&nbsp;<code>k<\/code>.<\/p>\n\n\n\n<p>We want to divide the array into exactly&nbsp;<code>n \/&nbsp;2<\/code>&nbsp;pairs such that the sum of each pair is divisible by&nbsp;<code>k<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>True<\/em>&nbsp;If you can find a way to do that or&nbsp;<em>False<\/em>&nbsp;otherwise.<\/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> arr = [1,2,3,4,5,10,6,7,8,9], k = 5\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> Pairs are (1,9),(2,8),(3,7),(4,6) and (5,10).\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> arr = [1,2,3,4,5,6], k = 7\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> Pairs are (1,6),(2,5) and(3,4).\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> arr = [1,2,3,4,5,6], k = 10\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong> You can try all possible pairs to see that there is no way to divide arr into 3 pairs each with sum divisible by 10.\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> arr = [-10,10], k = 2\n<strong>Output:<\/strong> true\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> arr = [-1,1,-2,2,-3,3,-4,4], k = 3\n<strong>Output:<\/strong> true\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>arr.length == n<\/code><\/li><li><code>1 &lt;= n &lt;= 10^5<\/code><\/li><li><code>n<\/code>&nbsp;is even.<\/li><li><code>-10^9 &lt;= arr[i] &lt;= 10^9<\/code><\/li><li><code>1 &lt;= k &lt;= 10^5<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Mod and Count<\/strong><\/h2>\n\n\n\n<p>Count the frequency of (x % k + k) % k.<br>f[0] should be even (zero is also even)<br>f[1] = f[k -1] ((1 + k &#8211; 1) % k == 0)<br>f[2] = f[k -2] ((2 + k &#8211; 2) % k == 0)<br>&#8230;<br>Time complexity: O(n)<br>Space complexity: O(k)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  bool canArrange(vector<int>& arr, int k) {\n    vector<int> f(k);\n    for (int x : arr) ++f[(x % k + k) % k];\n    for (int i = 1; i < k; ++i)\n      if (f[i] != f[k - i]) return false;\n    return f[0] % 2 == 0;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of integers&nbsp;arr&nbsp;of even length&nbsp;n&nbsp;and an integer&nbsp;k. We want to divide the array into exactly&nbsp;n \/&nbsp;2&nbsp;pairs such that the sum of each pair&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[24,31,177,158],"class_list":["post-6996","post","type-post","status-publish","format-standard","hentry","category-math","tag-frequency","tag-math","tag-medium","tag-mod","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6996","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=6996"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6996\/revisions"}],"predecessor-version":[{"id":6998,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6996\/revisions\/6998"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}