{"id":9535,"date":"2022-03-04T20:01:49","date_gmt":"2022-03-05T04:01:49","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9535"},"modified":"2022-03-04T20:02:37","modified_gmt":"2022-03-05T04:02:37","slug":"leetcode-2176-count-equal-and-divisible-pairs-in-an-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-2176-count-equal-and-divisible-pairs-in-an-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2176. Count Equal and Divisible Pairs in an Array"},"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><code>(i, j)<\/code><em>where<\/em><code>0 &lt;= i &lt; j &lt; n<\/code>,&nbsp;<em>such that<\/em><code>nums[i] == nums[j]<\/code><em>and<\/em><code>(i * j)<\/code><em>is divisible by<\/em><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> nums = [3,1,2,2,2,1,3], k = 2\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong>\nThere are 4 pairs that meet all the requirements:\n- nums[0] == nums[6], and 0 * 6 == 0, which is divisible by 2.\n- nums[2] == nums[3], and 2 * 3 == 6, which is divisible by 2.\n- nums[2] == nums[4], and 2 * 4 == 8, which is divisible by 2.\n- nums[3] == nums[4], and 3 * 4 == 12, which is 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 = 1\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> Since no value in nums is repeated, there are no pairs (i,j) that meet all the requirements.\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;= 100<\/code><\/li><li><code>1 &lt;= nums[i], k &lt;= 100<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Brute Force<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n<sup>2<\/sup>)<br>Space complexity: O(1)<\/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  int countPairs(vector<int>& nums, int k) {\n    const int n = nums.size();\n    int ans = 0;\n    for (int i = 0; i < n; ++i)\n      for (int j = i + 1; j < n; ++j)\n        ans += (nums[i] == nums[j]) &#038;&#038; (i * j % k == 0);\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(i, j)where0 &lt;= i &lt; j &lt; n,&nbsp;such thatnums[i] == nums[j]and(i * j)is divisible byk. Example 1:&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[413,20,106,769,222],"class_list":["post-9535","post","type-post","status-publish","format-standard","hentry","category-array","tag-all-pairs","tag-array","tag-brute-force","tag-divisible","tag-easy","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9535","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=9535"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9535\/revisions"}],"predecessor-version":[{"id":9537,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9535\/revisions\/9537"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}