id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
530 | <p>Given the <code>root</code> of a Binary Search Tree (BST), return <em>the minimum absolute difference between the values of any two different nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg" style="width: 292px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [4,2,6,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg" style="width: 282px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [1,0,48,null,null,12,49]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>4</sup>]</code>.</li>
<li><code>0 <= Node.val <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Note:</strong> This question is the same as 783: <a href="https://leetcode.com/problems/minimum-distance-between-bst-nodes/" target="_blank">https://leetcode.com/problems/minimum-distance-between-bst-nodes/</a></p>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void inorder(TreeNode* root, vector<int> &arr){\n if(root==nullptr){\n return ;\n }\n inorder(root->left,arr);\n arr.push_back(root->val);\n inorder(root->right,arr);\n\n }\n int getMinimumDifference(TreeNode* root) {\n vector<int> arr;\n int m = INT_MAX;\n inorder(root,arr);\n for(int i=1; i<arr.size(); i++){\n m = min(m,arr[i]-arr[i-1]);\n }\n // cout<<m;\n return m;\n }\n};",
"memory": "24400"
} |
530 | <p>Given the <code>root</code> of a Binary Search Tree (BST), return <em>the minimum absolute difference between the values of any two different nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg" style="width: 292px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [4,2,6,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg" style="width: 282px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [1,0,48,null,null,12,49]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>4</sup>]</code>.</li>
<li><code>0 <= Node.val <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Note:</strong> This question is the same as 783: <a href="https://leetcode.com/problems/minimum-distance-between-bst-nodes/" target="_blank">https://leetcode.com/problems/minimum-distance-between-bst-nodes/</a></p>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> nums;\n void inorder(TreeNode* root, vector<int>&nums){\n if(root==NULL){\n return ;\n }\n inorder(root->left, nums);\n nums.push_back(root->val);\n inorder(root->right, nums);\n }\n int getMinimumDifference(TreeNode* root) {\n inorder(root, nums);\n int cnt = INT_MAX;\n for(int i=1; i<nums.size(); i++){\n int diff = abs(nums[i]-nums[i-1]);\n cnt = min(cnt, diff); \n }\n return cnt;\n }\n};",
"memory": "24400"
} |
530 | <p>Given the <code>root</code> of a Binary Search Tree (BST), return <em>the minimum absolute difference between the values of any two different nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg" style="width: 292px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [4,2,6,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg" style="width: 282px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [1,0,48,null,null,12,49]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>4</sup>]</code>.</li>
<li><code>0 <= Node.val <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Note:</strong> This question is the same as 783: <a href="https://leetcode.com/problems/minimum-distance-between-bst-nodes/" target="_blank">https://leetcode.com/problems/minimum-distance-between-bst-nodes/</a></p>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int>v;\n void update_min(TreeNode* root) {\n if(!root) return;\n if(root->left) update_min(root->left) ;\n v.push_back(root->val);\n if(root->right) update_min(root->right) ;\n }\n int getMinimumDifference(TreeNode* root) {\n update_min(root);\n int n = v.size();\n int global_min = INT_MAX;\n for(int i = 1;i<n;i++) {\n global_min = min(global_min,v[i]-v[i-1]);\n }\n return global_min;\n }\n};",
"memory": "24500"
} |
530 | <p>Given the <code>root</code> of a Binary Search Tree (BST), return <em>the minimum absolute difference between the values of any two different nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg" style="width: 292px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [4,2,6,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg" style="width: 282px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [1,0,48,null,null,12,49]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>4</sup>]</code>.</li>
<li><code>0 <= Node.val <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Note:</strong> This question is the same as 783: <a href="https://leetcode.com/problems/minimum-distance-between-bst-nodes/" target="_blank">https://leetcode.com/problems/minimum-distance-between-bst-nodes/</a></p>
| 2 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void inorder(TreeNode* root, vector<int> &arr){\n if(root==nullptr){\n return ;\n }\n inorder(root->left,arr);\n arr.push_back(root->val);\n inorder(root->right,arr);\n\n }\n int getMinimumDifference(TreeNode* root) {\n vector<int> arr;\n int m = INT_MAX;\n inorder(root,arr);\n for(int i=1; i<arr.size(); i++){\n m = min(m,arr[i]-arr[i-1]);\n }\n // cout<<m;\n return m;\n }\n};",
"memory": "24500"
} |
530 | <p>Given the <code>root</code> of a Binary Search Tree (BST), return <em>the minimum absolute difference between the values of any two different nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg" style="width: 292px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [4,2,6,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg" style="width: 282px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [1,0,48,null,null,12,49]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>4</sup>]</code>.</li>
<li><code>0 <= Node.val <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Note:</strong> This question is the same as 783: <a href="https://leetcode.com/problems/minimum-distance-between-bst-nodes/" target="_blank">https://leetcode.com/problems/minimum-distance-between-bst-nodes/</a></p>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void inorder(TreeNode*root, vector<int> &v) {\n if (root == NULL) {\n return ; \n }\n inorder(root->left, v); \n v.push_back(root->val); \n inorder(root->right, v); \n }\n int getMinimumDifference(TreeNode* root) {\n if(root == NULL) return 0; \n vector<int> v; \n\n inorder(root, v); \n\n int ans = abs(v[0]-v[1]) ; \n\n for(int i=1; i<v.size()-1; i++) {\n ans = min(ans, abs(v[i]-v[i+1])); \n }\n return ans; \n }\n};\n\n////// 1, 2, 3, 4, 6",
"memory": "24600"
} |
530 | <p>Given the <code>root</code> of a Binary Search Tree (BST), return <em>the minimum absolute difference between the values of any two different nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg" style="width: 292px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [4,2,6,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg" style="width: 282px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [1,0,48,null,null,12,49]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>4</sup>]</code>.</li>
<li><code>0 <= Node.val <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Note:</strong> This question is the same as 783: <a href="https://leetcode.com/problems/minimum-distance-between-bst-nodes/" target="_blank">https://leetcode.com/problems/minimum-distance-between-bst-nodes/</a></p>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n void traverse(TreeNode* root, vector<int>& v) {\n if(!root) {\n return;\n }\n\n v.push_back(root->val);\n traverse(root->left, v);\n traverse(root->right, v);\n }\npublic:\n int getMinimumDifference(TreeNode* root) {\n vector<int> vals;\n\n traverse(root, vals);\n sort(vals.begin(), vals.end());\n\n int min_dist = INT_MAX;\n for(int i = 0; i < vals.size() - 1; ++i) {\n if(vals[i + 1] - vals[i] < min_dist) {\n min_dist = vals[i + 1] - vals[i];\n }\n }\n\n return min_dist;\n }\n};",
"memory": "24600"
} |
530 | <p>Given the <code>root</code> of a Binary Search Tree (BST), return <em>the minimum absolute difference between the values of any two different nodes in the tree</em>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst1.jpg" style="width: 292px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [4,2,6,1,3]
<strong>Output:</strong> 1
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/05/bst2.jpg" style="width: 282px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [1,0,48,null,null,12,49]
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>4</sup>]</code>.</li>
<li><code>0 <= Node.val <= 10<sup>5</sup></code></li>
</ul>
<p> </p>
<p><strong>Note:</strong> This question is the same as 783: <a href="https://leetcode.com/problems/minimum-distance-between-bst-nodes/" target="_blank">https://leetcode.com/problems/minimum-distance-between-bst-nodes/</a></p>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void inorder(TreeNode* root, vector<int>&v)\n {\n if(root==NULL)\n {\n return;\n }\n inorder(root->left,v);\n v.push_back(root->val);\n inorder(root->right,v);\n }\n int getMinimumDifference(TreeNode* root) {\n if(root==NULL)\n {\n return 0;\n }\n vector<int>v;\n inorder(root,v);\n sort(v.begin(),v.end());\n int ans=abs(v[0]-v[1]);\n for(int i=2;i<v.size();i++)\n {\n ans=min(ans,abs(v[i]-v[i-1]));\n }\n return ans;\n }\n};",
"memory": "24700"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n\n int n = nums.size();\n sort(nums.begin(), nums.end());\n\n int i = 0, j = 1;\n int ans = 0;\n\n \n\n while (j < n) {\n if ((nums[j] - nums[i]) == k) {\n while((i<(n-1)) && (nums[i] == nums[i+1])) {\n i++;\n }\n while((j<(n-1)) && (nums[j] == nums[j+1])) {\n j++;\n }\n i++;\n j++;\n if(i==j) j++;\n ans++;\n } else if ((nums[j] - nums[i]) > k) {\n if(i<(j-1))\n i++;\n else j++;\n } else {\n\n j++;\n }\n }\n\n return ans;\n }\n};",
"memory": "13700"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n\n int n = nums.size();\n sort(nums.begin(), nums.end());\n\n int i = 0, j = 1;\n int ans = 0;\n\n if (k == 0) {\n for (int i = 0; i < nums.size() - 1; i++) {\n if (nums[i] == nums[i + 1]) {\n while ((i < (n-1)) && (nums[i] == nums[i + 1])) {\n i++;\n }\n ans++;\n }\n }\n return ans;\n }\n\n while (j < n) {\n if ((nums[j] - nums[i]) == k) {\n while((i<(n-1)) && (nums[i] == nums[i+1])) {\n i++;\n }\n while((j<(n-1)) && (nums[j] == nums[j+1])) {\n j++;\n }\n i++;\n j++;\n ans++;\n } else if ((nums[j] - nums[i]) > k) {\n i++;\n } else {\n j++;\n }\n }\n\n return ans;\n }\n};",
"memory": "13800"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n sort(nums.begin(),nums.end());\n int n = nums.size();\n int count = 0;\n for (int i = 0; i < n; i++) {\n \n //unqiue\n if(i>0 && nums[i]==nums[i-1]){\n continue;\n }\n for (int j = i + 1; j <= n-1; j++) {\n if (abs( nums[j] - nums[i] ) == k) {\n count++;\n break;\n }\n }\n \n }\n return count;\n }\n};",
"memory": "13900"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int binarysearch(vector<int>& vec, int num, int min) {\n int low = min + 1, high = vec.size() - 1, mid;\n while (high >= low) {\n mid = (high + low) / 2;\n if (num > vec[mid]) {\n low = mid + 1;\n } else if (num < vec[mid]) {\n high = mid - 1;\n } else {\n return mid;\n }\n }\n return -1;\n }\n\n int findPairs(vector<int>& nums, int k) {\n sort(nums.begin(), nums.end());\n int count = 0, temp;\n for (int i = 0; i < nums.size(); ++i) {\n if (i != 0 && nums[i - 1] == nums[i]) {\n continue;\n }\n temp = binarysearch(nums, nums[i] + k, i);\n if (temp != -1) {\n count++;\n }\n }\n return count;\n }\n};",
"memory": "14000"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int binarysearch(vector<int>& vec, int num, int min) {\n int low = min + 1, high = vec.size() - 1, mid;\n while (high >= low) {\n mid = (high + low) / 2;\n if (num > vec[mid]) {\n low = mid + 1;\n } else if (num < vec[mid]) {\n high = mid - 1;\n } else {\n return mid;\n }\n }\n return -1;\n }\n\n int findPairs(vector<int>& nums, int k) {\n ios_base :: sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n sort(nums.begin(), nums.end());\n int count = 0, temp;\n for (int i = 0; i < nums.size(); ++i) {\n if (i != 0 && nums[i - 1] == nums[i]) {\n continue;\n }\n temp = binarysearch(nums, nums[i] + k, i);\n if (temp != -1) {\n count++;\n }\n }\n return count;\n }\n};",
"memory": "14000"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int target) {\n if(target < 0) return 0;\n int start = 0;\n int end = 1;\n int n = nums.size();\n int count = 0;\nsort(nums.begin() , nums.end());\n while(end < n){\n \n if(nums[end] - nums[start] > target){\n start ++;\n }\n else if( nums[end] - nums[start] < target || start == end ){\n end ++;\n }else{\n count ++;\n start ++;\n end ++;\n while(end < n && nums[end] == nums[end - 1]){\n end ++;\n }\n }\n\n \n }\n return count;\n }\n};",
"memory": "14100"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n sort(nums.begin(), nums.end());\n int left = 0, right = 1, count = 0;\n while (right < nums.size()) {\n if (left == right || nums[right] - nums[left] < k) {\n right++;\n } else if (nums[right] - nums[left] > k) {\n left++;\n } else {\n count++;\n left++;\n right++;\n // Skip duplicates\n while (right < nums.size() && nums[right] == nums[right - 1]) right++;\n }\n }\n return count;\n }\n};",
"memory": "14100"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n sort(nums.begin(),nums.end());\n\n int k_diff_pairs = 0;\n\n pair<int,int> last_pair = {INT_MIN,INT_MIN};\n\n int i=0;\n int j=1;\n\n while(j<nums.size()){\n if(i==j)j++;\n else if(nums[j]-nums[i]==k && last_pair.first != nums[i] && last_pair.second!=nums[j]){\n last_pair = {nums[i],nums[j]};\n k_diff_pairs++; \n j++;\n }\n else if(nums[j]-nums[i]>k){\n i++;\n }\n else{\n j++;\n }\n }\n\n return k_diff_pairs;\n }\n};",
"memory": "14200"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n \n sort(nums.begin(),nums.end());\n int i=0;\n int j=i+1;\n int n=nums.size();\n int cnt=0;\n\n\n while(j<n)\n {\n if(nums[j]-nums[i]==k)\n {\n i++;\n j++;\n cnt++;\n\n while(j<n && nums[j]==nums[j-1])\n {\n j++;\n }\n\n while(i<j && nums[i]==nums[i-1])\n {\n i++;\n }\n if(i==j)j++;\n } \n else if(nums[j]-nums[i]<k)\n {\n j++;\n }\n else\n {\n i++;\n if(i==j)\n {\n j++;\n }\n }\n }\n\n\n\n return cnt;\n\n\n\n\n\n }\n};",
"memory": "14200"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n \n int ans=0;\n if(k==0){\n unordered_map<int,int> mp;\n for(auto i: nums) mp[i]++;\n for(auto i: mp) if(i.second>1) ans++;\n return ans;\n }\n sort(nums.begin(),nums.end());\n \n nums.erase(unique(nums.begin(),nums.end()),nums.end());\n int n=nums.size();\n \n int i=1,j=0;\n while(i< n && j<n && i>j){\n int diff=nums[i]-nums[j];\n if(diff==k){\n ans++;\n j++,i++;\n continue;\n }\n else if(diff<k){\n i++;\n }\n else{\n cout<<\"hello\";\n while(j<n && nums[i]-nums[j]>k && j<=i){\n j++;\n }\n if(i==j)\n i=i+1;\n }\n }\n return ans; \n }\n};",
"memory": "14300"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& v1, int k) {\n sort(v1.begin(),v1.end());\n int ans=0;\n vector<int>v;\n if(k==0) {\n for(int i=0;i<v1.size()-1;i++) {\n int k=i+1;\n while(k<v1.size()&&v1[k]==v1[i]) {\n k++;\n }\n if(k!=i+1) {\n ans++;\n }\n \n i=k-1;\n }\n return ans;\n }\n for(int i=0;i<v1.size();i++) {\n if(i&&v1[i]==v1[i-1]) {\n continue;\n }\n v.push_back(v1[i]);\n }\n \n for(int i=0;i<v.size();i++) {\n if(i&&v[i]==v[i-1]) {\n continue;\n }\n \n auto j1=lower_bound(v.begin()+i+1,v.end(),v[i]+k);\n auto j2=upper_bound(v.begin()+i+1,v.end(),v[i]+k);\n if(j1==v.end()) {\n continue;\n }\n else {\n if(j2==v.end()) {\n \n ans+=v.size()-(j1-v.begin());\n } else {\n \n ans+=(j2-v.begin())-(j1-v.begin());\n }\n }\n\n }\n return ans;\n }\n};",
"memory": "14400"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n sort(nums.begin() , nums.end());\n int i = 0; \n int j = 1;\n vector<pair<int,int>> v;\n while (i< nums.size()){\n j = i+1;\n while(i+1<nums.size() && nums[i] == nums[i+1]){\n i++;\n }\n while(j < nums.size() && nums[i]+k >= nums[j]){\n if(nums[i]+ k == nums[j]){\n pair<int,int> m =make_pair(i,j);\n v.push_back(m);\n break;\n }\n else{\n j++;\n }\n }\n i++;\n }\n // for(int i=0 ; i<v.size)\n return v.size();\n }\n};",
"memory": "14500"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n sort(nums.begin(),nums.end());\n int count =0;\n vector<pair<int,int>>store;\n for(int i=0; i<nums.size(); i++){\n for(int j=i+1; j<nums.size(); j++){\n if(abs(nums[j]-nums[i]) == k){\n pair<int,int>target = {nums[i],nums[j]};\n if(find(store.begin(),store.end(),target) == store.end()){\n store.push_back(target);\n count++;\n }\n }\n }\n }\n for(auto i : store){\n cout<<i.first<<\",\";\n cout<<i.second<<\" \";\n }\n cout<<endl;\n return count;\n }\n};",
"memory": "14600"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n sort(nums.begin(),nums.end());\n int count =0;\n vector<pair<int,int>>store;\n for(int i=0; i<nums.size(); i++){\n for(int j=i+1; j<nums.size(); j++){\n if(abs(nums[j]-nums[i]) == k){\n pair<int,int>target = {nums[i],nums[j]};\n if(find(store.begin(),store.end(),target) == store.end()){\n store.push_back(target);\n count++;\n }\n }\n }\n }\n for(auto i : store){\n cout<<i.first<<\",\";\n cout<<i.second<<\" \";\n }\n cout<<endl;\n return count;\n }\n};",
"memory": "14700"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n int sum=1;\n int count =0;\n int n = nums.size();\n sort(nums.begin() , nums.end());\n vector<pair<int,int>> hey;\n for(int i=0; i<n; i++){\n \n \n int rem = nums[i] + k;\n int j = n-1;\n while( j>=0 && nums[j] > rem ){\n j--;\n }\n \n if (nums[j] == rem && i != j && !std::count(hey.begin(), hey.end(), make_pair(nums[i],nums[j])) ) {\n hey.push_back(make_pair(nums[i],nums[j]));\n sum++;\n }\n \n }\n return sum-1 + count/2;\n }\n};",
"memory": "14800"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n set<pair<int,int>> st;\n int n=nums.size();\n for(int i=0;i<n-1;i++){\n for(int j=i+1;j<n;j++){\n if(abs(nums[i]-nums[j])==k){\n st.insert({min(nums[i], nums[j]), max(nums[i], nums[j])});\n }\n }\n }\n return st.size();\n\n \n // unordered_map<int, int> freq;\n // int count = 0;\n \n // // Store the frequency of each number in the array\n // for (int num : nums) {\n // freq[num]++;\n // }\n \n // // Traverse the map and check for valid pairs\n // for (auto& [num, freq_count] : freq) {\n // if (k == 0) {\n // // If k is 0, we need to find elements that occur more than once\n // if (freq_count > 1) {\n // count++;\n // }\n // } else {\n // // Check if there exists a pair (num, num + k)\n // if (freq.find(num + k) != freq.end()) {\n // count++;\n // }\n // }\n // }\n \n // return count;\n \n }\n};",
"memory": "15400"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n int count=0;\n set<pair<int,int>>mpp;\n for(int i=0;i<nums.size()-1;i++) \n {for(int j=i+1;j<nums.size();j++)\n {if(i!=j && abs(nums[i]-nums[j])==k)\n { int a= min(nums[i],nums[j]);\n int b=max(nums[i],nums[j]);\n mpp.insert({a,b});}\n }\n } \n for(auto it:mpp)\n {count++;}\n return count;\n }\n};",
"memory": "15500"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& a, int k) {\n int n = size(a), res = 0;\n set<pair<int, int>> st;\n for(int i = 0; i < n - 1; i++){\n for(int j = i + 1; j < n; j++){\n if(abs(a[i] - a[j]) == k){\n st.insert({min(a[i], a[j]), max(a[i], a[j])});\n }\n }\n }\n return size(st);\n }\n};",
"memory": "15600"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n int count = 0;\n set<pair<int, int>> st;\n sort(nums.begin(), nums.end());\n int i=0, j=1;\n while(j<nums.size()){\n if(nums[j] - nums[i] == k) {\n st.insert({nums[i], nums[j]});\n i++;\n j++;\n } else if(nums[j] - nums[i] > k){\n i++;\n } else {\n j++;\n }\n if(i==j) j++;\n }\n return st.size();\n }\n};",
"memory": "16000"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n\n sort(nums.begin(), nums.end());\n set<pair<int,int>> s;\n int count = 0;\n int i = 0;\n int j = 1;\n while (j < nums.size()) {\n int diff = nums[j] - nums[i];\n if (diff == k) {\n s.insert({nums[i],nums[j]});\n ++i;\n ++j;\n }\n else if (diff < k){\n j++;\n }\n else {\n i++;\n }\n if(i==j){\n j++;\n }\n }\n return s.size();\n }\n};",
"memory": "16000"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\nint bs(vector<int>&nums , int s , int x){\n int e = nums.size()-1;\n \n while(s<=e){\n int mid = (s+e)/2;\n if(nums[mid]==x){\n return mid;\n }\n if(nums[mid]>x){\n e =mid-1;\n }\n else{\n s=mid+1;\n }\n }\n return -1;\n}\n int findPairs(vector<int>& nums, int k) {\n //two pointer approach\n // sort(nums.begin() , nums.end());\n // set<pair<int , int>>ans;\n // int i = 0,j=1;\n // while(j<nums.size());\n // int diff = nums[j]-nums[i];\n // if(diff==k){\n // cout<<nums[i]<<\" \"<<nums[j]<<endl;\n // ans.insert({nums[i] , nums[j]});\n // i++ ,j++;\n // }\n // else if(diff>k){\n // i++;\n // }\n // else if(diff<k){\n // j++;\n // }\n // if(i==j){j++;}\n // return ans.size();\n\n\n\n sort(nums.begin() , nums.end());\n set<pair<int , int>>ans;\n for(int i = 0; i<nums.size() ; i++){\n if(bs(nums , i+1, nums[i]+k)!= -1){\n ans.insert({nums[i] , nums[i]+k});\n }\n }\nreturn ans.size();\n }\n \n};\n ",
"memory": "16100"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n sort(nums.begin(), nums.end());\n set<pair<int,int >> ans;\n int i=0;int j= 1;\n \n while(j<nums.size()){\n if (nums[j]-nums[i] == k){\n ans.insert({nums[i],nums[j]});\n i++;\n j++;\n } else if(nums[j]-nums[i] > k){\n i++;\n } else if (nums[j]-nums[i] < k ){\n j++;\n }\n if (i==j) j++;\n }\n \n return ans.size();\n }\n};",
"memory": "16100"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int bs(vector<int>&arr,int s,int target)\n {\n int e = arr.size()-1;\n while(s<=e)\n {\n int mid = s + (e-s)/2;\n if(target == arr[mid]) return 1;\n else if(arr[mid]<target) s = mid + 1;\n else e = mid -1;\n }\n return -1;\n }\n int Pair(vector<int>& arr, int k)\n {\n int difference;\n int count = 0;\n int preI = INT_MIN, preJ = INT_MIN;\n int i = 0, j = i+1;\n set<pair<int,int>>tem;\n for(int i = 0 ; i < arr.size();i++)\n {\n int el = arr[i]+k;\n if(bs(arr,i+1,el)!=-1) tem.insert({arr[i],arr[i]+k});\n }\n return tem.size();\n }\n \n int findPairs(vector<int>& nums, int k) {\n sort(nums.begin(),nums.end());\n \n return Pair(nums,k);\n }\n};",
"memory": "16200"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n sort(nums.begin(),nums.end());\n int n=nums.size();\n set<pair<int,int>>st;\n\n int i=0;\n int j=1;\n \n\n while(j<n){\n if(nums[j]-nums[i]==k){\n st.insert({nums[j],nums[i]});\n i++;\n j++;\n\n }\n else if(nums[j]-nums[i]>k){\n i++;\n }\n else j++;\n if(i==j){\n j++;\n }\n }\n return st.size();\n\n \n }\n};",
"memory": "16200"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\nint twopointers(vector<int>nums,int k){\n\n set<pair<int,int>>ans;\n sort(nums.begin(),nums.end());\n int i=0,j=1;\n\n while(j<nums.size()){\n int diff=abs(nums[i]-nums[j]);\n if(diff==k){\n ans.insert({nums[i],nums[j]});\n i++,j++;\n }\n else if(diff>k)\n i++;\n\n else //diff<k\n j++;\n\n if(i==j) //given cond i!=j\n j++;\n \n\n }\n return ans.size();\n}\n\nint bruteforce(vector<int>nums,int k){\n \n set<pair<int,int>>ans; //so that only unique pair will store\n sort(nums.begin(),nums.end());\n\n for(int i=0;i<nums.size();i++){\n for(int j=i+1;j<nums.size();j++) {\n\n if(abs(nums[i]-nums[j])==k){\n ans.insert({nums[i],nums[j]});\n }\n } \n }\n int kdiff=ans.size(); //total size will be no of paris it stores\n return kdiff;\n }\n\n int findPairs(vector<int>& nums, int k) {\n\n int ans=twopointers(nums,k);\n return ans;\n }\n\n};",
"memory": "16300"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\nint bruteforce(vector<int>nums,int k){\n \n set<pair<int,int>>ans;\n sort(nums.begin(),nums.end());\n\n for(int i=0;i<nums.size();i++){\n for(int j=i+1;j<nums.size();j++) {\n\n if(abs(nums[i]-nums[j])==k){\n ans.insert({nums[i],nums[j]});\n }\n } \n }\n int kdiff=ans.size();\n return kdiff;\n }\n\n int findPairs(vector<int>& nums, int k) {\n\n int ans=bruteforce(nums,k);\n return ans;\n }\n\n};",
"memory": "16400"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n sort(nums.begin(), nums.end());\n int n = nums.size();\n unordered_set<int>visited;\n int ans=0;\n for(int i=1;i<nums.size();i++){\n if(visited.find(nums[i])!=visited.end()) continue;\n auto pl = lower_bound(nums.begin(), nums.begin()+i, nums[i]-k);\n if((pl!=nums.begin()+i)&&((*pl)==nums[i]-k)) ans++, visited.insert(nums[i]);\n }\n return ans;\n }\n};",
"memory": "16500"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k);\n};\n/*******************************************************************/\nint Solution::findPairs(vector<int>& nums, int k) {\n int count=0;\n if (k == 0) {\n int i, size=nums.size();\n map<int,int> m;\n map<int,int>::iterator itm;\n for (i = 0; i < size; ++i) {\n ++m[nums[i]];\n }\n for (itm = m.begin(); itm != m.end(); ++itm) {\n if (itm->second > 1) {\n ++count;\n }\n }\n return count;\n }\n set<int> s(nums.begin(), nums.end());\n set<int>::iterator it1, it2, ref1 = s.end(), ref2;\n --ref1;\n for (it1 = s.begin(); it1 != ref1; ++it1) {\n ref2 = it1;\n ++ref2;\n for(it2 = ref2; it2 != s.end(); ++it2) {\n if (*it2-*it1 == k) {\n ++count;\n } else if (*it2-*it1 > k ) {\n break;\n }\n }\n }\n return count;\n}\n/*******************************************************************/",
"memory": "16600"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n \n unordered_map<int,int>m;\n int ans = 0;\n\n sort(nums.begin(),nums.end());\n\n for(int i=0;i<nums.size();i++){\n\n if(k>0){\n\n if(m.count(nums[i]-k)>0){\n ans++;\n m.erase(nums[i]-k);\n }\n \n m[nums[i]]++;\n \n }\n else{\n if(m[nums[i]]==1){\n ans++;\n }\n m[nums[i]]++;\n }\n }\n\n return ans;\n\n }\n};",
"memory": "16700"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n unordered_map<int, int> mapper;\n for (auto el:nums){\n mapper[el]++;\n }\n int c=0;\n for (auto el: mapper){\n if (k>0 and mapper.find(el.first+k)!=mapper.end()) c++;\n // if (k>0 and mapper[el-k]) c++;\n else if (k==0 and mapper[el.first]>1) c++;\n // mapper[el]=false;\n }\n return c;\n }\n};",
"memory": "16800"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n if(k==0){\n int ans = 0;\n unordered_map<int,int> mp;\n for(auto i: nums)mp[i]++;\n for(auto i: mp){\n if(i.second>1)ans++;\n }\n return ans;\n }\n set<int> s;\n int ans = 0;\n sort(nums.begin(), nums.end());\n for(int i=0; i<nums.size(); i++){\n if(s.find(nums[i])!=s.end())continue;\n if(s.find(nums[i]-k)!=s.end())ans++;\n s.insert(nums[i]);\n }\n return ans;\n }\n};",
"memory": "16800"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n if (k < 0) return 0;\n unordered_map<int, int> freq;\n int count = 0;\n\n for (int num: nums) {\n ++freq[num];\n }\n\n if (k == 0) {\n for (const auto& pair: freq) {\n if (pair.second > 1) {\n ++count;\n }\n }\n } else {\n for (const auto& pair: freq) {\n if (freq.find(pair.first + k) != freq.end()) {\n ++count;\n }\n }\n }\n return count;\n }\n};",
"memory": "16900"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n int result = 0;\n unordered_map<int, int> counter;\n\n // Count occurrences of each number in the array\n for (int n : nums) {\n counter[n]++;\n }\n\n // Iterate over each element in the hashmap\n for (auto& entry : counter) {\n int x = entry.first;\n int val = entry.second;\n\n // Case when k > 0, we check if (x + k) exists in the map\n if (k > 0 && counter.find(x + k) != counter.end()) {\n result++;\n } \n // Case when k == 0, we check if there are more than 1 occurrence of x\n else if (k == 0 && val > 1) {\n result++;\n }\n }\n \n return result;\n }\n};",
"memory": "16900"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n unordered_map<int,int> mp;\n for(int i=0;i<nums.size();i++)\n {\n mp[nums[i]]++;\n }\n int ans=0;\n for(auto it:mp)\n {\n if(k==0 && it.second>=2)\n ans++;\n else\n if(k!=0 && mp.find(it.first+k)!=mp.end())\n {\n ans++;\n }\n }\n return ans;\n }\n};",
"memory": "17000"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n int result = 0;\n unordered_map<int, int> counter;\n // Count occurrences of each number in the array\n for (int n : nums) {\n counter[n]++;\n }\n // Iterate over each element in the hashmap\n for (auto entry : counter) {\n int x = entry.first;\n int val = entry.second;\n\n // Case when k > 0, we check if (x + k) exists in the map\n if (k > 0 && counter.find(x + k) != counter.end()) {\n result++;\n } \n // Case when k == 0, we check if there are more than 1 occurrence of x\n else if (k == 0 && val > 1) {\n result++;\n }\n }\n \n return result;\n }\n};",
"memory": "17000"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n unordered_map<int, int> numsMap;\n\n for (int n : nums) {\n numsMap[n]++;\n }\n\n int count = 0;\n for (int i = 0; i < nums.size(); i++) {\n int comp = k + nums[i];\n\n if (numsMap.find(comp) != numsMap.end()) {\n if (comp == nums[i] && numsMap[comp] == 1) {\n continue;\n }\n count++;\n }\n numsMap.erase(comp);\n }\n\n return count;\n }\n};\n",
"memory": "17100"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 2 | {
"code": "class Solution {\n public:\n int findPairs(vector<int>& nums, int k) {\n int ans = 0;\n unordered_map<int, int> numToIndex;\n\n for (int i = 0; i < nums.size(); ++i)\n numToIndex[nums[i]] = i;\n\n for (int i = 0; i < nums.size(); ++i) {\n const int target = nums[i] + k;\n if (const auto it = numToIndex.find(target);\n it != numToIndex.cend() && it->second != i) {\n ++ans;\n numToIndex.erase(target);\n }\n }\n\n return ans;\n }\n};",
"memory": "17100"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& data, int x) {\n std::unordered_map<int, int> dict;\n for (int i = 0; i < std::ssize(data); ++i) {\n dict[data[i]]++;\n }\n int64_t count = 0;\n for (auto[key, value]: dict) {\n if (x == 0) {\n if (value > 1) {\n count++;\n }\n } else {\n if (dict.find(key + x) != dict.end()) {\n count++;\n }\n }\n }\n return count;\n }\n};",
"memory": "17200"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int solveForZero(vector<int>& nums,int k){\n unordered_map<int,int> freq;\n for(auto num:nums){\n freq[num]++;\n }\n int ans= 0;\n for(auto x:freq){\n if(x.second>1) ans++;\n }\n return ans;\n }\n int findPairs(vector<int>& nums, int k) {\n if(k==0){\n return solveForZero(nums,k);\n }\n sort(nums.begin(),nums.end());\n unordered_set<int> present;\n int ans = 0;\n for(int i = 0;i<nums.size();i++){\n if(i!=0 && nums[i]==nums[i-1]) continue;\n int target = nums[i]-k;\n if(present.find(target)!=present.end()) ans++;\n present.insert(nums[i]);\n } \n return ans;\n }\n};",
"memory": "17200"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n vector<int> arr;\n unordered_set<int> myset(nums.begin(),nums.end());\n unordered_set<int> myset1;\n unordered_set<int> myset2;\n if(k==0){\n for(int i=0;i<nums.size();i++){\n if(myset2.find(nums[i])==myset2.end()){\n myset2.insert(nums[i]);\n }\n else{\n myset1.insert(nums[i]);\n }\n }\n return myset1.size();\n }\n for(auto i:myset){\n arr.push_back(i);\n }\n int count=0;\n for(int i =0;i<arr.size()-1;i++){\n for(int j=i+1;j<arr.size();j++){\n if(abs(arr[i]-arr[j])==k){\n count++;\n }\n }\n }\n return count;\n }\n};",
"memory": "17300"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& a, int k) {\n map<int,int>mp;\n for(auto &x:a)\n {\n mp[x]++;\n }\n int ans=0;\n while(mp.size())\n {\n int val=mp.begin()->first;\n if(k)\n {\n if(mp.find(val+k)!=mp.end())\n {\n ans+=1;\n }\n }\n else\n {\n ans+=(mp[val]>=2); \n }\n mp.erase(mp.begin());\n }\n return ans;\n }\n};",
"memory": "17400"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n map<int,int> mp ; \n\n for(auto& it:nums){\n mp[it]++ ; \n }\n\n int ans = 0 ; \n\n for(auto& it:mp){\n\n if(k == 0){\n if(it.second > 1){\n ans++ ; \n }\n }\n else{\n int target = k + it.first ; \n if(mp.find(target) != mp.end()){\n ans++ ; \n }\n }\n }\n\n return ans ; \n\n\n }\n};",
"memory": "17500"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& data, int x) {\n std::unordered_map<int, int64_t> dict;\n for (int i = 0; i < std::ssize(data); ++i) {\n dict[data[i]]++;\n }\n int64_t count = 0;\n for (auto[key, value]: dict) {\n if (x == 0) {\n if (value > 1) {\n count++;\n }\n } else {\n if (dict.find(key + x) != dict.end()) {\n count++;\n }\n }\n }\n return count;\n }\n};",
"memory": "17600"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n unordered_map<long long int,int>mp;\n for(int i=0;i<nums.size();i++){\nmp[nums[i]+pow(10,7)]++;\n\n }\nint c=0;\n//cout<<mp.size();\n\nfor(auto el:mp){\n // cout<<el.second;\n \n if(mp.find(k+el.first)!=mp.end()){\n//c++;continue;\n\nif(k==0&&mp[k+el.first]>1){c++; }\n else if(k!=0) {c++;}\n \n}\n \n \n \n\n}\nreturn c;\n\n\n }\n};",
"memory": "17600"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n map<int, int> mp;\n \n for (auto n : nums) {\n mp[n]++;\n }\n\n\n int count = 0;\n for (auto e : mp) {\n \n if (k == 0) {\n if (e.second > 1) count++;\n } else {\n if (mp.find(e.first-k) != mp.end()) {\n count++;\n }\n }\n }\n return count;\n }\n};",
"memory": "17700"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n sort(nums.begin(),nums.end());\n set<vector<int>>s; \n int i=0,j=1;\n\n while(j<nums.size()){\n\n if(i==j){\n j++;\n } \n\n else if((nums[j]-nums[i])==k){\n s.insert({nums[i],nums[j]});\n i++;j++;\n }\n else if((nums[j]-nums[i])<k){\n j++;\n }\n else{\n i++;\n }\n }\n return s.size();\n }\n};",
"memory": "17800"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n\n int cnt=0;\n sort(nums.begin(),nums.end());\n set<pair<int,int>>st;\n for(int i=0;i<nums.size();i++)\n {\n for(int j=0;j<nums.size();j++)\n {\n if(i!=j && abs(nums[i]-nums[j])==k && st.find({nums[i],nums[j]})==st.end()) \n {\n cnt++;\n st.insert({nums[i],nums[j]});\n st.insert({nums[j],nums[i]});\n }\n if(abs(nums[j]-nums[i])<k) break;\n }\n }\n return cnt;\n }\n};",
"memory": "17900"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n\n int cnt=0;\n sort(nums.begin(),nums.end());\n set<pair<int,int>>st;\n int j=1,i=0;\n while(j<nums.size())\n {\n if(i!=j && abs(nums[i]-nums[j])==k && st.find({nums[i],nums[j]})==st.end())\n {\n cnt++;\n st.insert({nums[i],nums[j]});\n st.insert({nums[j],nums[i]});\n i++;j++;\n } \n else if(abs(nums[i]-nums[j]) > k && i<j)\n {\n i++;\n\n }\n else j++;\n }\n return cnt;\n }\n};",
"memory": "17900"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n int n=nums.size();\n sort(nums.begin(),nums.end());\n set<vector<int>> s;\n int l=0,r=1;\n int cnt=0;\n while(r<n){\n if(nums[r]-nums[l]==k){\n s.insert({nums[r],nums[l]});\n l++;\n r++;\n }\n else if(nums[r]-nums[l]>k){\n l++;\n }\n else{\n r++;\n }\n if(l==r){\n r++;\n }\n }\n return s.size();\n }\n};",
"memory": "18000"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n \n int twoPointerApp(vector<int> &nums, int k ){\n // step 01: sort the array\n sort(nums.begin(), nums.end());\n int i = 0;\n int j = 1;\n set<pair<int, int>>ans;\n\n // step 02: use two pointers and compare\n\n while(j<nums.size()){\n if(nums[j] - nums[i] == k){\n ans.insert({nums[i], nums[j]});\n i++;\n j++;\n }\n else if(nums[j] - nums[i] > k){\n i++;\n }\n else{\n j++;\n }\n if(i == j) j++;\n } \n\n return ans.size();\n }\n\n int binarySearchApp(vector<int> &nums, int start, int k){\n int end = nums.size() -1;\n int mid = start + (end-start)/2;\n\n while(start <= end){\n if(nums[mid] == k){\n return mid;\n }\n else if(nums[mid] > k){\n end = mid - 1;\n }\n else{\n start = mid + 1;\n }\n mid = start + (end - start)/2;\n }\n return -1;\n\n }\n int findPairs(vector<int>& nums, int k) {\n\n int twoPointerAns = twoPointerApp(nums, k);\n // return twoPointerAns;\n\n\n set<pair<int,int>>binarySearchAns;\n\n for(int i = 0; i< nums.size(); i++){\n if((binarySearchApp(nums, i+1, nums[i] + k)) != -1){\n binarySearchAns.insert({nums[i] , nums[i] + k});\n }\n }\n return binarySearchAns.size();\n \n }\n};",
"memory": "18100"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n if (k < 0) return 0; // k must be non-negative\n \n if (k == 0) {\n unordered_map<int, int> freqMap;\n int count = 0;\n \n for (int num : nums) {\n freqMap[num]++;\n }\n \n for (const auto& [num, freq] : freqMap) {\n if (freq > 1) {\n count++;\n }\n }\n \n return count;\n }\n \n // Handle k > 0\n unordered_set<int> numSet;\n unordered_set<int> pairSet;\n \n for (int num : nums) {\n numSet.insert(num);\n }\n \n for (int num : numSet) {\n if (numSet.count(num + k)) {\n pairSet.insert(num);\n }\n }\n \n return pairSet.size();\n }\n};",
"memory": "18500"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n /*\n O(nlogn) - Sort nums\n Create hashSet\n Iterate through sorted nums\n Pair ONLY with # that has already been seen (smaller than curr #)\n \n O(n) - Create hashMap { int -> hashSet<int> }\n Iterate through nums\n For each #, look for # - k or # + k\n If # - k or # + k doesn't exist in hashMap[#], add it\n Iterate through all entries in hashMap to create res\n\n */\n int n = nums.size();\n unordered_map<int, int> count;\n int res = 0;\n for (unsigned int i = 0; i < n; ++i) {\n if (k == 0 && count[nums[i]] == 1) {\n ++res;\n } else if (count[nums[i]] == 0) {\n if (count[nums[i]+k] > 0) ++res;\n if (count[nums[i]-k] > 0) ++res;\n }\n\n ++count[nums[i]];\n }\n return res;\n }\n};",
"memory": "18600"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n unordered_set<int> vals;\n unordered_set<int> accepted_vals;\n int ans = 0;\n\n for(int num: nums) {\n if(vals.find(num + k) != vals.end() && accepted_vals.find(num) == accepted_vals.end()) {\n ans++;\n accepted_vals.insert(num);\n }\n if(vals.find(num - k) != vals.end() && accepted_vals.find(num - k) == accepted_vals.end()) {\n ans++;\n accepted_vals.insert(num - k);\n }\n vals.insert(num);\n }\n\n return ans;\n }\n};",
"memory": "18700"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int K) {\n int k = (long long int ) K;\n set<pair<int,int>> st;\n sort(nums.begin() , nums.end());\n int n = nums.size();\n set<long long int> s;\n for(int i = 0 ; i< n ; i++){\n if(s.find(nums[i]) != s.end()){\n st.insert({nums[i], nums[i] - k});\n }\n s.insert((long long int)nums[i]+k);\n }\n return st.size();\n }\n};",
"memory": "18800"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n unordered_map<int,int>mp;\n set<pair<int,int>>st;\n for(auto it:nums){\n int ele=it;\n if(mp.find(it+k)!=mp.end()){\n st.insert({it,it+k});\n }\n if(mp.find(it-k)!=mp.end()){\n st.insert({it-k,it});\n }\n mp[it]++;\n }\n return st.size();\n }\n};",
"memory": "18800"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n int n = nums.size();\n int count = 0;\n set<vector<int>> s;\n for(int i=0;i<n;i++){\n for(int j=i+1;j<n;j++){\n int diff = abs(nums[i]-nums[j]);\n if(diff==k){\n if(nums[i]<nums[j]){\n if(s.find({nums[i], nums[j]})==s.end()){\n {\n s.insert({nums[i], nums[j]});\n count++;\n }\n }\n }else{\n if(s.find({nums[j], nums[i]})==s.end()){\n s.insert({nums[j], nums[i]});\n count++;\n }\n }\n }\n }\n }\n return count;\n }\n};",
"memory": "18900"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n \n unordered_map<int,int> us;\n map<pair<int,int>,int> ans;\n\n sort(nums.begin(),nums.end());\n int n=nums.size();\n int cnt=0;\n for(int i=0;i<n;i++)\n {\n us[nums[i]]++;\n }\n if(k==0)\n {\n for(auto x:us)\n {\n if(x.second>1)cnt++;\n }\n return cnt;\n }\n\n for(int i=0;i<n;i++)\n {\n if(us.find(nums[i]+k)!=us.end())\n {\n ans[{nums[i],nums[i]+k}]++;\n }\n }\n return ans.size();\n\n\n\n\n\n\n\n\n\n\n }\n};",
"memory": "18900"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n unordered_map<int, bool> visited;\n visited[nums[0]] = false;\n set<pair<int, int>> pairs;\n for(int i=1;i<nums.size();i++){\n if(visited.find(nums[i]-k)!=visited.end()){\n pair<int, int> p;\n if(nums[i]<nums[i]-k)\n p = make_pair(nums[i], nums[i]-k);\n else\n p = make_pair(nums[i]-k, nums[i]);\n pairs.insert(p);\n }\n if(visited.find(nums[i]+k)!=visited.end()){\n pair<int, int> p;\n if(nums[i]<nums[i]+k)\n p = make_pair(nums[i], nums[i]+k);\n else\n p = make_pair(nums[i]+k, nums[i]);\n pairs.insert(p);\n }\n visited[nums[i]] = false;\n }\n return pairs.size();\n }\n};",
"memory": "19000"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n \n unordered_map<int,int> us;\n map<pair<int,int>,int> ans;\n\n sort(nums.begin(),nums.end());\n int n=nums.size();\n int cnt=0;\n for(int i=0;i<n;i++)\n {\n us[nums[i]]++;\n }\n if(k==0)\n {\n for(auto x:us)\n {\n if(x.second>1)cnt++;\n }\n return cnt;\n }\n\n for(int i=0;i<n;i++)\n {\n if(us.find(nums[i]+k)!=us.end())\n {\n ans[{nums[i],nums[i]+k}]++;\n }\n }\n return ans.size();\n\n\n\n\n\n\n\n\n\n\n }\n};",
"memory": "19100"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nint findPairs(vector<int>& nums, int k) {\n int ans = 0;\n unordered_set<int> st; \n unordered_map<int, int> mp;\n for(int t : nums) {\n st.insert(t);\n mp[t] += 1;\n } \n for(int t : st) {\n if(st.count(t));\n int d = t + k;\n if(d == t) {\n if(mp[t] > 1) {\n ans += 1;\n }\n } else if(st.count(d)) {\n ans += 1;\n }\n }\n return ans;\n}\n};",
"memory": "19200"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "#include <unordered_set>\n#include <unordered_map>\n\nclass Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n int count = 0;\n\n if (k == 0) {\n std::unordered_map<int, int> dict;\n for (auto num : nums) {\n if (!dict.contains(num)) {\n dict[num] = 0;\n } else {\n dict[num]++;\n }\n }\n\n for (auto p: dict) {\n if (p.second >= 1) {\n count++;\n }\n }\n\n return count;\n }\n std::unordered_set<int> numSet;\n for (auto num : nums) \n numSet.insert(num);\n\n std::unordered_set<int> st;\n\n for (auto num : numSet) {\n count += static_cast<int>(st.contains(num + k)) + static_cast<int>(st.contains(num - k));\n st.insert(num);\n }\n \n return count;\n }\n};",
"memory": "19300"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n set<pair<int, int>> st;\n map<int, int> mp;\n for(auto it : nums){\n if(mp.find(it + k) != mp.end()){\n int p = min(it, it + k);\n int q = max(it, it + k);\n st.insert({p, q});\n }\n if(mp.find(it - k) != mp.end()){\n int p = min(it, it - k);\n int q = max(it, it - k);\n st.insert({p, q});\n }\n mp[it]++;\n }\n\n return st.size();\n }\n};",
"memory": "19400"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n int ans = 0;\n map<pair<int, int>, int> m;\n map<int, int> m1;\n for (int i = 0; i < nums.size(); i++) {\n m1[nums[i]]++;\n }\n int c = 0;\n for (auto it : m1) {\n if (k == 0) {\n if (it.second > 1) {\n c++;\n }\n continue;\n }\n if (m1.find(it.first + k) != m1.end()) {\n m[{it.first, it.first + k}]++;\n }\n if (m1.find(it.first - k) != m1.end()) {\n m[{it.first - k, it.first}]++;\n }\n }\n for (auto it : m) {\n cout << it.first.first << \" \" << it.first.second << endl;\n }\n return m.size() + c;\n }\n};",
"memory": "19500"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n map<pair<int,int>,int> mp;\n unordered_map<int,int> hash;\n int ans=0;\n for(int i=0; i<nums.size(); i++) {\n hash[nums[i]]++;\n }\n if(k==0) {\n for(int i=0; i<nums.size(); i++) {\n if(hash[nums[i]]>=2)\n ans+=1;\n hash[nums[i]]=0;\n }\n return ans;\n }\n for(int i=0; i<nums.size(); i++) {\n int req = nums[i]-k;\n if(hash[req]>0) {\n pair<int,int> p={req,nums[i]};\n if(req>nums[i]) {\n p.first=nums[i];\n p.second=req;\n }\n if(mp[p]==0){\n cout << p.first << \" \" << p.second << endl;\n ans++;\n }\n //ans++;\n mp[p]++;\n }\n }\n return ans;\n }\n};",
"memory": "19600"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n sort(nums.begin(), nums.end());\n unordered_map<int, bool> store;\n map<pair<int, int>, bool> store2;\n int ans = 0;\n for(int i = 0;i < nums.size();i++){\n int remain = nums[i] - k;\n if(store[remain] && store2.find({nums[i], remain}) == store2.end()){\n ans++;\n store2[{nums[i], remain}] = true; \n } \n store[nums[i]] = true; \n }\n return ans;\n }\n};",
"memory": "19700"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n sort(nums.begin(), nums.end());\n unordered_map<int, int> m;\n set<pair<int, int>> s;\n int ans = 0;\n for(auto i : nums) {\n int target = i - k;\n if(s.count({i, target}) == 0 && m[target] != 0) {\n ans += 1;\n s.insert({i, target});\n }\n m[i] = 1;\n }\n return ans;\n }\n};",
"memory": "19800"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n set <vector<int>> st;\n for(int i=0;i<nums.size()-1;i++){\n for(int j=i+1;j<nums.size();j++){\n if(abs(nums[i]-nums[j])==k){\n vector <int> vec={nums[i],nums[j]};\n sort(vec.begin(),vec.end());\n st.insert(vec);\n }\n }\n }\n return st.size();\n }\n};",
"memory": "19900"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n int n = nums.size();\n int count = 0;\n set<vector<int>> s;\n for(int i=0;i<n;i++){\n for(int j=i+1;j<n;j++){\n int diff = abs(nums[i]-nums[j]);\n if(diff==k){\n if(s.find({nums[i], nums[j]})==s.end() && s.find({nums[j], nums[i]})==s.end()){\n {\n s.insert({nums[i], nums[j]});\n count++;\n }\n }\n }\n }\n }\n return count;\n }\n};",
"memory": "20000"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n sort(nums.begin(),nums.end());\n set<vector<int>> s;\n int i =0;\n int j =1;\n while(i<nums.size()&&j<nums.size()){\n int diff = nums[j]-nums[i];\n if(diff==k){\n vector<int> v;\n v.push_back(nums[i]);\n v.push_back(nums[j]);\n s.insert(v);\n i++;j++;\n }else if(diff>k){\n i++;\n }else{\n j++;\n }\n if(i==j) j++;\n }\n return s.size();\n}\n \n \n};",
"memory": "20100"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& arr, int k) {\n set<vector<int>> s; \n \n\n for(int i =0;i<arr.size();++i){\n for(int j =i+1;j<arr.size();j++){\n if(abs(arr[i]-arr[j])==k){\n vector<int> v;\n if(arr[i]<=arr[j]){\n v.push_back(arr[i]);\n v.push_back(arr[j]);\n\n }else{\n v.push_back(arr[j]);\n v.push_back(arr[i]);\n\n }\n \n \n s.insert(v);\n }\n }\n }\n return s.size();\n \n }\n};",
"memory": "20200"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic: \n int findPairs(vector<int>& nums, int k) {\n unordered_map<int, int> map;\n set<tuple<int, int>> result;\n \n for(auto num: nums){\n if (map[num-k] > 0 ){\n result.insert({num - k, num});\n }\n \n if (map[num+k] > 0 ){\n result.insert({num, num + k});\n }\n \n map[num] += 1;\n }\n \n return result.size();\n }\n};\n",
"memory": "20300"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n int count = 0;\n map <vector<int>, int> mp;\n sort(nums.begin(),nums.end());\n for(int i = 0;i<nums.size();i++){\n if((nums[nums.size()-1]-nums[i])<k) break;\n for(int j = nums.size()-1;j>i;j--){\n if(nums[j]-nums[i] == k){\n mp[{nums[j],nums[i]}]++;\n if( mp[{nums[j], nums[i]}]<=1) count++;\n }\n else if(nums[j]-nums[i]<k) break;\n }\n }\n return count;\n }\n};",
"memory": "20400"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& a, int k) {\n\n int ans=0;\n if(k==0){\n map<int,int>mp1;\n for(auto i:a)mp1[i]++;\n for(auto i:mp1)if(i.second>1)ans++;\n return ans;\n }\n map<int,int>mp;\n set<int>st;\n for(auto i:a)st.insert(i);\n a.clear();\n for(auto i:st)a.push_back(i);\n int n=a.size();\n // sort(a.begin(),a.end());\n mp.clear();\n for(int i=0;i<n;i++){\n if(mp.count(a[i]-k)){\n // cout<<a[i]<<endl;\n ans++;}\n mp[a[i]]=1;\n }\n // for(auto i:st)cout<<i.first<<\" \"<<i.second<<endl;\n return ans;\n }\n};",
"memory": "20500"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n int n = nums.size();\n sort(nums.begin(),nums.end());\n\n unordered_set<int> vals;\n unordered_set<string> pairs;\n\n for(int i=0;i<n;i++)\n {\n if(vals.find(nums[i]-k)!=vals.end())\n {\n string s1 = to_string(nums[i]);\n string s2 = to_string(nums[i]-k);\n pairs.insert(s2+s1);\n\n }\n vals.insert(nums[i]);\n }\n\n return (int)pairs.size();\n }\n};",
"memory": "20500"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n unordered_set<int> arr;\n unordered_set<string> pair;\n int n = nums.size();\n int count = 0;\n for (int i = 0; i < n; i++){\n if (arr.find(nums[i]+k) != arr.end()){\n string temp = to_string(nums[i]) + '#' + to_string(nums[i]+k);\n if (pair.find(temp) == pair.end()){\n count++;\n pair.insert(temp);\n }\n }\n if (arr.find(nums[i]-k) != arr.end()){\n string temp = to_string(nums[i]-k) + '#' + to_string(nums[i]);\n if (pair.find(temp) == pair.end()){\n count++;\n pair.insert(temp);\n }\n }\n arr.insert(nums[i]);\n }\n return count;\n }\n};",
"memory": "20600"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "void __print(int x) {cout << x;} void __print(long x) {cout << x;} void __print(long long x) {cout << x;} void __print(unsigned x) {cout << x;} void __print(unsigned long x) {cout << x;} void __print(unsigned long long x) {cout << x;} void __print(float x) {cout << x;} void __print(double x) {cout << x;} void __print(long double x) {cout << x;} void __print(char x) {cout << '\\'' << x << '\\'';} void __print(const char *x) {cout << '\\\"' << x << '\\\"';} void __print(const string &x) {cout << '\\\"' << x << '\\\"';} void __print(bool x) {cout << (x ? \"true\" : \"false\");} template<typename T, typename V> void __print(const pair<T, V> &x) {cout << '{'; __print(x.first); cout << ','; __print(x.second); cout << '}';}template<typename T> void __print(const T &x) {int f = 0; cout << '{'; for (auto &i: x) cout << (f++ ? \",\" : \"\"), __print(i); cout << \"}\";} void _print() {cout << \"]\\n\";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cout << \", \"; _print(v...);}\n#define debug(x...) cout << \"[\" << #x << \"] = [\"; _print(x)\nvoid db(const vector<bool>& v) { bool first = true; string res = \"{\"; for (int i = 0; i < int(v.size()); i++) { if (!first) res += \", \"; first = false; res += to_string(v[i]); } res += \"}\"; cout<<res<<'\\n';\n}\nclass Solution {\npublic:\n int findPairs(vector<int>& a, int k) {\n int n = size(a);\n map<int, int> cnt;\n for(int x: a){\n cnt[x]++;\n }\n if(k == 0){\n int res = 0;\n for(auto [x, y]: cnt){\n if(y >= 2){\n res++;\n }\n }\n return res;\n }\n int res = 0;\n vector<int> b;\n set<int> st;\n for(auto [x, y]: cnt){\n b.push_back(x);\n st.insert(x); \n }\n for(int x: b){\n int need = x - k;\n if(st.count(need)){\n res++;\n }\n // |x - need| = k\n // x - need = k\n // need - x = k\n need = x + k;\n if(st.count(need)){\n res++;\n }\n }\n return res / 2;\n }\n};",
"memory": "20700"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n map<int,int> m;\n map<pair<int,int>,int> s;\n for(int i=0;i<nums.size();i++){\n if(m[nums[i]-k]>0){\n //cout<<i<<\" \";\n s[{max(nums[i]-k,nums[i]),min(nums[i]-k,nums[i])}]++;\n }\n if(m[k+nums[i]]>0){\n cout<<i<<\" \";\n s[{max(k+nums[i],nums[i]),min(k+nums[i],nums[i])}]++;\n }\n m[nums[i]]++;\n }\n return s.size();\n }\n};",
"memory": "20800"
} |
532 | <p>Given an array of integers <code>nums</code> and an integer <code>k</code>, return <em>the number of <b>unique</b> k-diff pairs in the array</em>.</p>
<p>A <strong>k-diff</strong> pair is an integer pair <code>(nums[i], nums[j])</code>, where the following are true:</p>
<ul>
<li><code>0 <= i, j < nums.length</code></li>
<li><code>i != j</code></li>
<li><code>|nums[i] - nums[j]| == k</code></li>
</ul>
<p><strong>Notice</strong> that <code>|val|</code> denotes the absolute value of <code>val</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [3,1,4,1,5], k = 2
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of <strong>unique</strong> pairs.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 1
<strong>Output:</strong> 4
<strong>Explanation:</strong> There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,1,5,4], k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is one 0-diff pair in the array, (1, 1).
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>4</sup></code></li>
<li><code>-10<sup>7</sup> <= nums[i] <= 10<sup>7</sup></code></li>
<li><code>0 <= k <= 10<sup>7</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findPairs(vector<int>& nums, int k) \n {\n int ans = 0;\n int n = nums.size();\n map<int,bool> mp;\n map<pair<int,int>,bool> vis;\n for(int i=0;i<n;i++)\n {\n if(mp[nums[i]+k] && !vis[{nums[i],nums[i]+k}])\n {\n ans += 1;\n vis[{nums[i],nums[i]+k}] = 1;\n }\n if(mp[nums[i]-k] && !vis[{nums[i]-k,nums[i]}])\n {\n ans += 1;\n vis[{nums[i]-k,nums[i]}] = 1;\n }\n mp[nums[i]]=1;\n }\n return ans;\n }\n};",
"memory": "20900"
} |
539 | Given a list of 24-hour clock time points in <strong>"HH:MM"</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> timePoints = ["23:59","00:00"]
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> timePoints = ["00:00","23:59","00:00"]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= timePoints.length <= 2 * 10<sup>4</sup></code></li>
<li><code>timePoints[i]</code> is in the format <strong>"HH:MM"</strong>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMinDifference(vector<string>& timePoints) {\n sort(begin(timePoints), end(timePoints));\n\n int res = INT_MAX;\n for (int i = 1; i < timePoints.size(); ++i) {\n res = min(res, getDiff(timePoints[i - 1], timePoints[i]));\n }\n if (timePoints.size() > 2) {\n res = min(res, getDiff(timePoints[0], timePoints.back()));\n }\n\n return res;\n }\n\nprivate:\n int getDiff(const string& a, const string& b) {\n int ma = getMinute(a);\n int mb = getMinute(b);\n\n const int MIN_IN_A_DAY = 24 * 60;\n int diff = min(mb - ma, (ma + MIN_IN_A_DAY - mb) % MIN_IN_A_DAY);\n return diff;\n }\n\n int getMinute(const string& t) {\n return ((t[0] - '0') * 10 + (t[1] - '0')) * 60 + ((t[3] - '0') * 10 + (t[4] - '0'));\n }\n};",
"memory": "16900"
} |
539 | Given a list of 24-hour clock time points in <strong>"HH:MM"</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> timePoints = ["23:59","00:00"]
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> timePoints = ["00:00","23:59","00:00"]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= timePoints.length <= 2 * 10<sup>4</sup></code></li>
<li><code>timePoints[i]</code> is in the format <strong>"HH:MM"</strong>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n // bool comparator(auto x, auto y){\n // if(x)\n // }\n int getT(string& s){\n return ((s[0]-'0')*10+s[1]-'0')*60+(s[3]-'0')*10+(s[4]-'0');\n }\n int findMinDifference(vector<string>& timePoints) {\n int n = timePoints.size();\n sort(timePoints.begin(), timePoints.end());\n int mindiff = INT_MAX;\n for(int i=0;i<n-1;i++){\n mindiff = min(mindiff, abs(getT(timePoints[i]) - getT(timePoints[i+1])));\n }\n mindiff = min(mindiff, abs(getT(timePoints[n-1]) - (getT(timePoints[0])+1440)));\n return mindiff;\n }\n};",
"memory": "17000"
} |
539 | Given a list of 24-hour clock time points in <strong>"HH:MM"</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> timePoints = ["23:59","00:00"]
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> timePoints = ["00:00","23:59","00:00"]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= timePoints.length <= 2 * 10<sup>4</sup></code></li>
<li><code>timePoints[i]</code> is in the format <strong>"HH:MM"</strong>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int toMinutes(string &s) {\n const int adj = (600 + 60 + 10 + 1) * '0'; \n return 600 * s[0] + 60 * s[1] + 10 * s[3] + s[4] - adj;\n }\n int findMinDifference(vector<string>& timePoints) {\n sort(timePoints.begin(), timePoints.end());\n int prev = -(1440 - toMinutes(timePoints.back()));\n int minDiff = INT_MAX;\n for (string &s : timePoints) {\n int cur = toMinutes(s);\n minDiff = min(minDiff, abs(cur - prev));\n prev = cur;\n }\n return minDiff;\n }\n};",
"memory": "17000"
} |
539 | Given a list of 24-hour clock time points in <strong>"HH:MM"</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> timePoints = ["23:59","00:00"]
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> timePoints = ["00:00","23:59","00:00"]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= timePoints.length <= 2 * 10<sup>4</sup></code></li>
<li><code>timePoints[i]</code> is in the format <strong>"HH:MM"</strong>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMinDifference(vector<string>& timePoints) {\n constexpr int kMaxTimePoints{24*60};\n vector<bool> occurred(kMaxTimePoints, false);\n for (const string &t : timePoints) {\n int hour = stoi(t.substr(0, 2));\n int minute = stoi(t.substr(3, 2));\n int moment = 60 * hour + minute;\n if (occurred[moment]) return 0;\n occurred[moment] = true;\n }\n int res{INT_MAX};\n int first{0}, last{kMaxTimePoints-1};\n while (first < kMaxTimePoints) {\n if (occurred[first]) break;\n ++first;\n }\n while (last > first) {\n if (occurred[last]) break;\n --last;\n }\n int pre{first};\n for (int i = first + 1; i <= last; ++i) {\n if (occurred[i]) {\n res = min(res, i - pre);\n pre = i;\n }\n }\n // cout << \"first = \" << first << \", last = \" << last << endl;\n return min(res, kMaxTimePoints + first - last);\n }\n};",
"memory": "17100"
} |
539 | Given a list of 24-hour clock time points in <strong>"HH:MM"</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> timePoints = ["23:59","00:00"]
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> timePoints = ["00:00","23:59","00:00"]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= timePoints.length <= 2 * 10<sup>4</sup></code></li>
<li><code>timePoints[i]</code> is in the format <strong>"HH:MM"</strong>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMinDifference(vector<string>& timePoints) {\n ranges::sort(timePoints);\n int ans = 1440;\n int prev = -1440;\n for(auto& s : timePoints){\n int cur = stoi(s.substr(0, 2)) * 60 + stoi(s.substr(3, 2));\n ans = min(ans, cur - prev);\n prev = cur;\n }\n int cur = stoi(timePoints[0].substr(0, 2)) * 60 + stoi(timePoints[0].substr(3, 2)) + 1440;\n return min(ans, cur - prev);\n }\n};",
"memory": "17100"
} |
539 | Given a list of 24-hour clock time points in <strong>"HH:MM"</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> timePoints = ["23:59","00:00"]
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> timePoints = ["00:00","23:59","00:00"]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= timePoints.length <= 2 * 10<sup>4</sup></code></li>
<li><code>timePoints[i]</code> is in the format <strong>"HH:MM"</strong>.</li>
</ul>
| 0 | {
"code": "class Solution {\n\n int toMinutes(const std::string& timePoint) {\n return ((timePoint[0] - '0') * 10 + timePoint[1] - '0') * 60 + ((timePoint[3] - '0') * 10 + timePoint[4] - '0');\n }\n\npublic:\n int findMinDifference(vector<string>& timePoints) {\n \n std::sort(timePoints.begin(), timePoints.end());\n int prev = toMinutes(timePoints[0]);\n int result = toMinutes(timePoints[0]) + 24 * 60 - toMinutes(*timePoints.rbegin());\n\n for(int i = 1; i < timePoints.size(); ++i) {\n int current = toMinutes(timePoints[i]); \n result = std::min(result, current - prev);\n prev = current;\n }\n\n\n return result;\n }\n};",
"memory": "17200"
} |
539 | Given a list of 24-hour clock time points in <strong>"HH:MM"</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> timePoints = ["23:59","00:00"]
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> timePoints = ["00:00","23:59","00:00"]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= timePoints.length <= 2 * 10<sup>4</sup></code></li>
<li><code>timePoints[i]</code> is in the format <strong>"HH:MM"</strong>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMinDifference(vector<string>& timePoints) {\n sort(timePoints.begin(), timePoints.end());\n int ans = INT_MAX;\n for (int i = 1; i < timePoints.size(); i++) {\n int h1 = (timePoints[i - 1][0] - '0') * 10 +\n (timePoints[i - 1][1] - '0');\n int h2 = (timePoints[i][0] - '0') * 10 + (timePoints[i][1] - '0');\n int m1 = (timePoints[i - 1][3] - '0') * 10 +\n (timePoints[i - 1][4] - '0');\n int m2 = (timePoints[i][3] - '0') * 10 + (timePoints[i][4] - '0');\n ans = min(ans, (((h2 - h1) * 60) + (m2 - m1)));\n }\n int n = timePoints.size();\n int h1 = (timePoints[0][0] - '0') * 10 + (timePoints[0][1] - '0');\n int h2 =\n (timePoints[n - 1][0] - '0') * 10 + (timePoints[n - 1][1] - '0');\n int m2 =\n (timePoints[n - 1][3] - '0') * 10 + (timePoints[n - 1][4] - '0');\n int m1 = (timePoints[0][3] - '0') * 10 + (timePoints[0][4] - '0');\n ans = min(ans, ((((23 - h2) + h1) * 60) + ((60 - m2 + m1))));\n return ans;\n }\n};",
"memory": "17200"
} |
539 | Given a list of 24-hour clock time points in <strong>"HH:MM"</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> timePoints = ["23:59","00:00"]
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> timePoints = ["00:00","23:59","00:00"]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= timePoints.length <= 2 * 10<sup>4</sup></code></li>
<li><code>timePoints[i]</code> is in the format <strong>"HH:MM"</strong>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMinDifference(vector<string>& timePoints) {\n vector<int > minarray(timePoints.size());\n for(int i=0; i < timePoints.size(); i++){\n string x = timePoints[i];\n string hs= x.substr(0,2);\n string ms= x.substr(3,2);\n\n int hm = stoi(hs) *60;\n int m = stoi(ms) + hm;\n\n minarray[i] = m;\n }\n \n sort(minarray.begin(),minarray.end());\n int mini = INT_MAX;\n for(int i=0;i< minarray.size()-1;i++){\n int d= minarray[i+1] - minarray[i];\n mini = min(d,mini);\n }\n \n mini= min(mini, (24*60 - minarray[minarray.size()-1]) + minarray[0]);\n \n\n return mini;\n }\n};",
"memory": "17300"
} |
539 | Given a list of 24-hour clock time points in <strong>"HH:MM"</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> timePoints = ["23:59","00:00"]
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> timePoints = ["00:00","23:59","00:00"]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= timePoints.length <= 2 * 10<sup>4</sup></code></li>
<li><code>timePoints[i]</code> is in the format <strong>"HH:MM"</strong>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMinDifference(vector<string>& timePoints) {\n int n = timePoints.size();\n vector<int> minutes(n);\n\n for(int i = 0; i < n; i++) {\n string time = timePoints[i];\n\n string hourSubstr = time.substr(0, 2); //\"HH\"\n string minSubstr = time.substr(3); //\"MM\"\n\n int hourInt = stoi(hourSubstr);\n int minInt = stoi(minSubstr);\n\n minutes[i] = hourInt*60 + minInt;\n }\n\n sort(begin(minutes), end(minutes));\n\n int result = INT_MAX;\n for(int i = 1; i < n; i++) {\n result = min(result, minutes[i] - minutes[i-1]);\n }\n\n return min(result, (24*60 - minutes[n-1]) + minutes[0]);\n }\n};\n",
"memory": "17300"
} |
539 | Given a list of 24-hour clock time points in <strong>"HH:MM"</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> timePoints = ["23:59","00:00"]
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> timePoints = ["00:00","23:59","00:00"]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= timePoints.length <= 2 * 10<sup>4</sup></code></li>
<li><code>timePoints[i]</code> is in the format <strong>"HH:MM"</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMinDifference(vector<string>& timePoints) {\n int res = 24*60;\n vector<int> temp(timePoints.size(),0);\n for(int i = 0 ; i < timePoints.size() ; i ++)\n temp[i] = stoi(timePoints[i].substr(0,2))*60 + stoi(timePoints[i].substr(3,2));\n sort(temp.begin(),temp.end());\n temp.push_back(temp[0]+res);\n for(int i = 1 ; i < temp.size() ; i ++)\n res = min(res,temp[i]-temp[i-1]);\n return res;\n }\n};",
"memory": "17700"
} |
539 | Given a list of 24-hour clock time points in <strong>"HH:MM"</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> timePoints = ["23:59","00:00"]
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> timePoints = ["00:00","23:59","00:00"]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= timePoints.length <= 2 * 10<sup>4</sup></code></li>
<li><code>timePoints[i]</code> is in the format <strong>"HH:MM"</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMinDifference(vector<string>& timePoints) {\n vector<int> m;\n string s;\n int cur;\n for(int i = 0;i < timePoints.size();i++){\n cur = 60*((timePoints[i][0]-'0')*10+(timePoints[i][1]-'0'))+ (timePoints[i][3]-'0')*10+(timePoints[i][4]-'0');\n m.push_back(cur);\n }\n sort(m.begin(),m.end());\n int minv=200000;\n for(int i = 0;i < m.size()-1;i++){\n minv = min(minv,m[i+1]-m[i]);\n }\n minv =min(minv,1440-m[m.size()-1]+m[0]);\n return minv;\n }\n};",
"memory": "17700"
} |
539 | Given a list of 24-hour clock time points in <strong>"HH:MM"</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> timePoints = ["23:59","00:00"]
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> timePoints = ["00:00","23:59","00:00"]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= timePoints.length <= 2 * 10<sup>4</sup></code></li>
<li><code>timePoints[i]</code> is in the format <strong>"HH:MM"</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMinDifference(vector<string>& timePoints) {\n vector<int> arr;\n for (auto str: timePoints) {\n int hours = (str[0] - '0') * 10 + (str[1] - '0'); \n int min = (str[3] - '0') * 10 + (str[4] - '0');\n\n int minutes = hours * 60 + min;\n arr.push_back(minutes);\n\n minutes += 24 * 60;\n arr.push_back(minutes);\n }\n\n sort(arr.begin(), arr.end());\n for (auto e: arr) {\n cout << e << endl;\n }\n\n\n int ans = INT_MAX;\n\n for (int i = 0; i < arr.size() - 1; ++i) {\n ans = min(ans, arr[i + 1] - arr[i]);\n }\n\n return ans;\n }\n};",
"memory": "17800"
} |
539 | Given a list of 24-hour clock time points in <strong>"HH:MM"</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> timePoints = ["23:59","00:00"]
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> timePoints = ["00:00","23:59","00:00"]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= timePoints.length <= 2 * 10<sup>4</sup></code></li>
<li><code>timePoints[i]</code> is in the format <strong>"HH:MM"</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int TFH = 1440;\n int findMinDifference(vector<string>& timePoints) {\n vector<int> v;\n for (string str : timePoints) {\n int time = p(str[0])*600+p(str[1])*60+p(str[3])*10+p(str[4]);\n v.push_back(time); v.push_back(time+TFH);\n }\n sort(v.begin(), v.end());\n\n int ret = 2e5;\n for (int i = 1; i < v.size(); i++) {\n ret = min(ret, v[i]-v[i-1]);\n }\n\n return ret;\n }\n\n int p(char c) {\n return c-'0';\n }\n};",
"memory": "17800"
} |
539 | Given a list of 24-hour clock time points in <strong>"HH:MM"</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> timePoints = ["23:59","00:00"]
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> timePoints = ["00:00","23:59","00:00"]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= timePoints.length <= 2 * 10<sup>4</sup></code></li>
<li><code>timePoints[i]</code> is in the format <strong>"HH:MM"</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n // time/space: O(nlogn)/O(n)\n int findMinDifference(vector<string>& timePoints) {\n int n = timePoints.size();\n vector<int> times;\n for (auto& tp : timePoints) {\n int time = ((tp[0] - '0') * 10 + (tp[1] - '0')) * 60 + ((tp[3] - '0') * 10 + (tp[4] - '0'));\n times.push_back(time);\n times.push_back(time + 24 * 60);\n }\n sort(times.begin(), times.end());\n\n int minDiff = INT_MAX;\n for (int i = 1 ; i < times.size(); i++) minDiff = min(minDiff, times[i] - times[i - 1]);\n return minDiff;\n }\n};",
"memory": "17900"
} |
539 | Given a list of 24-hour clock time points in <strong>"HH:MM"</strong> format, return <em>the minimum <b>minutes</b> difference between any two time-points in the list</em>.
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> timePoints = ["23:59","00:00"]
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> timePoints = ["00:00","23:59","00:00"]
<strong>Output:</strong> 0
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= timePoints.length <= 2 * 10<sup>4</sup></code></li>
<li><code>timePoints[i]</code> is in the format <strong>"HH:MM"</strong>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMinDifference(vector<string>& timePoints) {\n vector<int> times;\n for (string &s : timePoints) {\n int hrs = stoi(s.substr(0, 2));\n int mins = stoi(s.substr(3, 2));\n times.push_back((24 + hrs) * 60 + mins);\n times.push_back(hrs * 60 + mins);\n }\n sort(times.begin(), times.end());\n int mn = INT_MAX;\n for (int i = 1; i < times.size(); i++) {\n mn = min(mn, times[i] - times[i - 1]);\n }\n return mn;\n }\n};",
"memory": "17900"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.