{"id":9531,"date":"2022-03-04T19:49:45","date_gmt":"2022-03-05T03:49:45","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9531"},"modified":"2022-03-04T19:50:19","modified_gmt":"2022-03-05T03:50:19","slug":"leetcode-2183-count-array-pairs-divisible-by-k","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-2183-count-array-pairs-divisible-by-k\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2183. Count Array Pairs Divisible by K"},"content":{"rendered":"\n<p>Given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>nums<\/code>&nbsp;of length&nbsp;<code>n<\/code>&nbsp;and an integer&nbsp;<code>k<\/code>, return&nbsp;<em>the&nbsp;<strong>number of pairs<\/strong><\/em>&nbsp;<code>(i, j)<\/code>&nbsp;<em>such that:<\/em><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>0 &lt;= i &lt; j &lt;= n - 1<\/code>&nbsp;<em>and<\/em><\/li><li><code>nums[i] * nums[j]<\/code>&nbsp;<em>is divisible by<\/em>&nbsp;<code>k<\/code>.<\/li><\/ul>\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 = [1,2,3,4,5], k = 2\n<strong>Output:<\/strong> 7\n<strong>Explanation:<\/strong> \nThe 7 pairs of indices whose corresponding products are divisible by 2 are\n(0, 1), (0, 3), (1, 2), (1, 3), (1, 4), (2, 3), and (3, 4).\nTheir products are 2, 4, 6, 8, 10, 12, and 20 respectively.\nOther pairs such as (0, 2) and (2, 4) have products 3 and 15 respectively, which are not divisible by 2.    \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 = [1,2,3,4], k = 5\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> There does not exist any pair of indices whose corresponding product is divisible by 5.\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], k &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Math<\/strong><\/h2>\n\n\n\n<p>a * b % k == 0 &lt;=> gcd(a, k) * gcd(b, k) == 0<\/p>\n\n\n\n<p>Use a counter of gcd(x, k) so far to compute the number of pairs.<\/p>\n\n\n\n<p>Time complexity: O(n*f), where f is the number of gcds, f &lt;= 128 for x &lt;= 1e5<br>Space complexity: O(<meta charset=\"utf-8\">f)<\/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  long long countPairs(vector<int>& nums, int k) {\n    unordered_map<int, int> m;\n    long long ans = 0;\n    for (int x : nums) {\n      long long d1 = gcd(x, k);\n      for (const auto& [d2, c] : m)\n        if (d1 * d2 % k == 0) ans += c;\n      ++m[d1];\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a&nbsp;0-indexed&nbsp;integer array&nbsp;nums&nbsp;of length&nbsp;n&nbsp;and an integer&nbsp;k, return&nbsp;the&nbsp;number of pairs&nbsp;(i, j)&nbsp;such that: 0 &lt;= i &lt; j &lt;= n &#8211; 1&nbsp;and nums[i] * nums[j]&nbsp;is divisible by&nbsp;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":[49],"tags":[359,217,31],"class_list":["post-9531","post","type-post","status-publish","format-standard","hentry","category-math","tag-gcd","tag-hard","tag-math","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9531","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=9531"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9531\/revisions"}],"predecessor-version":[{"id":9534,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9531\/revisions\/9534"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}