{"id":8763,"date":"2021-11-25T12:42:56","date_gmt":"2021-11-25T20:42:56","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8763"},"modified":"2021-11-25T12:43:25","modified_gmt":"2021-11-25T20:43:25","slug":"leetcode-2006-count-number-of-pairs-with-absolute-difference-k","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-2006-count-number-of-pairs-with-absolute-difference-k\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2006. Count Number of Pairs With Absolute Difference K"},"content":{"rendered":"\n<p>Given an integer array&nbsp;<code>nums<\/code>&nbsp;and an integer&nbsp;<code>k<\/code>, return&nbsp;<em>the number of pairs<\/em>&nbsp;<code>(i, j)<\/code>&nbsp;<em>where<\/em>&nbsp;<code>i &lt; j<\/code>&nbsp;<em>such that<\/em>&nbsp;<code>|nums[i] - nums[j]| == k<\/code>.<\/p>\n\n\n\n<p>The value of&nbsp;<code>|x|<\/code>&nbsp;is defined as:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>x<\/code>&nbsp;if&nbsp;<code>x &gt;= 0<\/code>.<\/li><li><code>-x<\/code>&nbsp;if&nbsp;<code>x &lt; 0<\/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,2,1], k = 1\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong> The pairs with an absolute difference of 1 are:\n- [<strong><u>1<\/u><\/strong>,<strong><u>2<\/u><\/strong>,2,1]\n- [<strong><u>1<\/u><\/strong>,2,<strong><u>2<\/u><\/strong>,1]\n- [1,<strong><u>2<\/u><\/strong>,2,<strong><u>1<\/u><\/strong>]\n- [1,2,<strong><u>2<\/u><\/strong>,<strong><u>1<\/u><\/strong>]\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,3], k = 3\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> There are no pairs with an absolute difference of 3.\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 = [3,2,1,5,4], k = 2\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> The pairs with an absolute difference of 2 are:\n- [<strong><u>3<\/u><\/strong>,2,<strong><u>1<\/u><\/strong>,5,4]\n- [<strong><u>3<\/u><\/strong>,2,1,<strong><u>5<\/u><\/strong>,4]\n- [3,<strong><u>2<\/u><\/strong>,1,5,<strong><u>4<\/u><\/strong>]\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;= 200<\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 100<\/code><\/li><li><code>1 &lt;= k &lt;= 99<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashtable<br><\/strong>|y &#8211; x| = k<br>y = x + k or y = x &#8211; k<\/h2>\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++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int countKDifference(vector<int>& nums, int k) {\n    unordered_map<int, int> m;\n    int ans = 0;\n    for (int x : nums) {\n      ans += m[x - k];\n      ans += m[x + k];\n      ++m[x];\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer array&nbsp;nums&nbsp;and an integer&nbsp;k, return&nbsp;the number of pairs&nbsp;(i, j)&nbsp;where&nbsp;i &lt; j&nbsp;such that&nbsp;|nums[i] &#8211; nums[j]| == k. The value of&nbsp;|x|&nbsp;is defined as: x&nbsp;if&nbsp;x &gt;=&#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":[222,82,269],"class_list":["post-8763","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-easy","tag-hashtable","tag-pairs","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8763","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=8763"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8763\/revisions"}],"predecessor-version":[{"id":8765,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8763\/revisions\/8765"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}