question_slug
stringlengths
3
77
title
stringlengths
1
183
slug
stringlengths
12
45
summary
stringlengths
1
160
author
stringlengths
2
30
certification
stringclasses
2 values
created_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
updated_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
hit_count
int64
0
10.6M
has_video
bool
2 classes
content
stringlengths
4
576k
upvotes
int64
0
11.5k
downvotes
int64
0
358
tags
stringlengths
2
193
comments
int64
0
2.56k
populating-next-right-pointers-in-each-node
Level Order Easy Implementation
level-order-easy-implementation-by-vaibh-xgv9
\nimport queue\nclass Solution:\n def connect(self, root: \'Optional[Node]\') -> \'Optional[Node]\':\n if root is None:\n return\n m
vaibhavsharma30
NORMAL
2022-02-19T17:24:55.144824+00:00
2022-02-19T17:25:20.084496+00:00
75
false
```\nimport queue\nclass Solution:\n def connect(self, root: \'Optional[Node]\') -> \'Optional[Node]\':\n if root is None:\n return\n myqueue=queue.Queue()\n myqueue.put(root)\n myqueue.put(None)\n small=[]\n while myqueue.empty()==False:\n front=myqueu...
6
0
[]
0
populating-next-right-pointers-in-each-node
Very easy BFS Solution C++ | Hint for constant Space
very-easy-bfs-solution-c-hint-for-consta-ra7a
Hint for constant space -> is to use the next links that you just created \n\nclass Solution {\npublic:\n Node* connect(Node* root) {\n if(root == NUL
Preacher001
NORMAL
2021-11-28T14:29:59.908584+00:00
2021-11-28T14:30:22.087713+00:00
286
false
Hint for constant space -> is to use the next links that you just created \n```\nclass Solution {\npublic:\n Node* connect(Node* root) {\n if(root == NULL) return root;\n queue<Node*> q;\n q.push(root);\n while(!q.empty()){\n int n=q.size();\n int j=0;\n f...
6
0
['Breadth-First Search', 'C']
1
populating-next-right-pointers-in-each-node
🔥 C++: O(1) space, O(n) time, 7 lines solution 🔥
c-o1-space-on-time-7-lines-solution-by-d-esli
\n/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), rig
duongdung12a8
NORMAL
2021-10-02T01:06:49.801641+00:00
2021-10-02T01:06:49.801692+00:00
400
false
```\n/*\n// Definition for a Node.\nclass Node {\npublic:\n int val;\n Node* left;\n Node* right;\n Node* next;\n\n Node() : val(0), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}\n\n Node(int _val, Node* _left, Node* _right, Node* _nex...
6
0
['Recursion', 'C', 'C++']
1
populating-next-right-pointers-in-each-node
JAVA Solution | 10 lines code | O(n) Time and Constant space
java-solution-10-lines-code-on-time-and-et8bk
Idea here to take advantage of already set values of next. When going to child, parent will already have their next value. We can take advantage of that. This i
arjun8900
NORMAL
2021-04-01T04:20:40.975984+00:00
2021-04-06T02:54:38.203746+00:00
158
false
Idea here to take advantage of already set values of next. When going to child, parent will already have their next value. We can take advantage of that. This is the minimize solution.\n\n```\npublic Node connect(Node root) {\n if(root == null) return root;\n modify(root);\n \n return root;\...
6
0
[]
0
populating-next-right-pointers-in-each-node
[Python] Jump Game III with queue (deque) + visited set
python-jump-game-iii-with-queue-deque-vi-sd9f
BFS to iterate through all indexes connected to starting index. Storing already explored indices allows us to ensure that we only explore each index a single ti
wookiewarlord
NORMAL
2020-11-29T09:21:58.229884+00:00
2020-11-29T09:21:58.229926+00:00
293
false
BFS to iterate through all indexes connected to starting index. Storing already explored indices allows us to ensure that we only explore each index a single time.\n\t\n\tdef canReach(self, arr, start):\n \n q = collections.deque()\n q.append(start)\n visited = set()\n \n while...
6
0
[]
0
populating-next-right-pointers-in-each-node
C++ || Constant Space || Iterative + Recursive
c-constant-space-iterative-recursive-by-l700c
Iteraitve Solution \n\n\nclass Solution {\npublic:\n Node* connect(Node* root) {\n if(!root || !root->left) return root;\n Node *current = root
rahu
NORMAL
2020-11-14T10:54:32.741128+00:00
2020-11-14T10:54:32.741162+00:00
307
false
Iteraitve Solution \n\n```\nclass Solution {\npublic:\n Node* connect(Node* root) {\n if(!root || !root->left) return root;\n Node *current = root;\n while(current->left) {\n Node *temp = current;\n while(current) {\n current->left->next = current->right;\n ...
6
0
['C', 'C++']
0
populating-next-right-pointers-in-each-node
5 Lines Recursive Python Solution🔥🔥🔥
5-lines-recursive-python-solution-by-dya-h2q0
\n\nclass Solution:\n def connect(self, root: \'Node\', next=None) -> \'Node\':\n if root is None: return None\n root.next = next\n self
dyang0829
NORMAL
2020-07-30T04:02:42.438231+00:00
2020-07-30T04:08:01.636809+00:00
391
false
\n```\nclass Solution:\n def connect(self, root: \'Node\', next=None) -> \'Node\':\n if root is None: return None\n root.next = next\n self.connect(root.left, root.right)\n self.connect(root.right, root.next.left if root.next else None)\n return root\n```
6
0
['Tree', 'Depth-First Search', 'Recursion', 'Python']
2
populating-next-right-pointers-in-each-node
go (golang): DFS & BFS
go-golang-dfs-bfs-by-mangreen-lu2l
DFS\ngolang\n/**\n * Definition for a Node.\n * type Node struct {\n * Val int\n * Left *Node\n * Right *Node\n * Next *Node\n * }\n */\n\nfunc
mangreen
NORMAL
2020-05-13T09:55:58.944558+00:00
2020-05-13T10:29:26.981517+00:00
164
false
1. DFS\n```golang\n/**\n * Definition for a Node.\n * type Node struct {\n * Val int\n * Left *Node\n * Right *Node\n * Next *Node\n * }\n */\n\nfunc connect(root *Node) *Node {\n if root == nil {\n return nil\n }\n \n if root.Left != nil {\n root.Left.Next = root.Right\n ...
6
0
[]
0
k-diff-pairs-in-an-array
Java O(n) solution - one Hashmap, easy to understand
java-on-solution-one-hashmap-easy-to-und-cna6
\npublic class Solution {\n public int findPairs(int[] nums, int k) {\n if (nums == null || nums.length == 0 || k < 0) return 0;\n \n
tankztc
NORMAL
2017-03-05T05:31:31.937000+00:00
2018-10-21T00:22:27.838050+00:00
68,306
false
```\npublic class Solution {\n public int findPairs(int[] nums, int k) {\n if (nums == null || nums.length == 0 || k < 0) return 0;\n \n Map<Integer, Integer> map = new HashMap<>();\n int count = 0;\n for (int i : nums) {\n map.put(i, map.getOrDefault(i, 0) + 1);\n ...
429
6
[]
58
k-diff-pairs-in-an-array
[Java/Python] Easy Understood Solution
javapython-easy-understood-solution-by-l-e1wq
Explanation\nCount the elements with Counter\nIf k > 0, for each element i, check if i + k exist.\nIf k == 0, for each element i, check if count[i] > 1\n\n\n# E
lee215
NORMAL
2017-03-10T13:34:45.483000+00:00
2020-08-18T03:14:20.857620+00:00
30,731
false
# **Explanation**\nCount the elements with `Counter`\nIf `k > 0`, for each element `i`, check if `i + k` exist.\nIf `k == 0`, for each element `i`, check if `count[i] > 1`\n<br>\n\n# **Explanation**\nTime `O(N)`\nSpace `O(N)`\n<br>\n\n**Python**\n```py\ndef findPairs(self, nums, k):\n res = 0\n c = collec...
347
7
[]
51
k-diff-pairs-in-an-array
C++ MULTIPLE APPROACHES : MAPS / TWO POINTER
c-multiple-approaches-maps-two-pointer-b-stlo
Hi , This problem is pretty straightforward . Let me explain the problem first. \n##### EXPLANATION : \nThe problem says that we are provided with an array of i
Krypto2_0
NORMAL
2022-02-09T01:51:56.404517+00:00
2022-02-09T03:22:31.551317+00:00
27,122
false
Hi , This problem is pretty straightforward . Let me explain the problem first. \n##### EXPLANATION : \nThe problem says that we are provided with an array of integers and we have to find out the ***Count of unique pairs*** in the array such that the ***absolute difference of elements of the pair is ==k.***\nMathematic...
220
4
['C', 'C++']
16
k-diff-pairs-in-an-array
An explanation Going from O(NlogN) -> O(N)
an-explanation-going-from-onlogn-on-by-h-3o9b
So, this problem is very similar to a very famous problem Two Sum problem. But a slightly different, there we have to only check wether a pair exists or not whi
hi-malik
NORMAL
2022-02-09T02:33:40.380577+00:00
2022-02-09T02:52:44.499695+00:00
11,816
false
So, this problem is very similar to a very famous problem `Two Sum` problem. But a slightly different, there we have to only check wether a pair exists or not which has 2 sum equals to the target. But here we have to count those such pairs & only consider the unique one.\n\n**Okay, so how we will solve this problem?**\...
141
74
[]
17
k-diff-pairs-in-an-array
✅ Well Explained || Two Easy Solutions ✅
well-explained-two-easy-solutions-by-mah-p1gl
First Approach : Using HashMap\n\n1. First we will create map for counting frequencies of each element in the array.\n2. Now we have 2 cases over here as \n
mahesh340
NORMAL
2022-02-09T02:55:22.011249+00:00
2022-02-09T07:10:20.726554+00:00
23,402
false
**First Approach : Using HashMap**\n\n1. First we will create map for counting frequencies of each element in the array.\n2. Now we have 2 cases over here as \n -->a) if k == 0 it means we need to count frequency of the same element by using map.get(i) method.\n\t-->b) we need to take counter approach for every elem...
132
3
['Binary Search', 'Sorting', 'Java']
12
k-diff-pairs-in-an-array
easy java solution, two HashSets O(n)
easy-java-solution-two-hashsets-on-by-lo-x8w1
\nclass Solution {\n public int findPairs(int[] nums, int k) {\n if (k < 0) return 0;\n Set<Integer> numbers = new HashSet<>();\n Set<In
lolozo
NORMAL
2018-06-27T04:20:47.129300+00:00
2018-06-27T04:20:47.129300+00:00
3,127
false
```\nclass Solution {\n public int findPairs(int[] nums, int k) {\n if (k < 0) return 0;\n Set<Integer> numbers = new HashSet<>();\n Set<Integer> found = new HashSet<>();\n for (int n : nums) {\n if (numbers.contains(n + k)) found.add(n);\n if (numbers.contains(n - k...
90
1
[]
10
k-diff-pairs-in-an-array
Two-pointer Approach
two-pointer-approach-by-lixx2100-f98o
The problem is just a variant of 2-sum.\nUpdate: Fixed a bug that can cause integer subtraction overflow.\nUpdate: The code runs in O(n log n) time, using O(1)
lixx2100
NORMAL
2017-03-05T07:47:25.627000+00:00
2018-10-16T04:42:07.140834+00:00
29,917
false
The problem is just a variant of 2-sum.\n**Update:** Fixed a bug that can cause integer subtraction overflow.\n**Update:** The code runs in `O(n log n)` time, using `O(1)` space.\n\n```java\npublic int findPairs(int[] nums, int k) {\n int ans = 0;\n Arrays.sort(nums);\n for (int i = 0, j = 0; i < nums.length; ...
76
11
[]
19
k-diff-pairs-in-an-array
C++ O(N) Time with unordered_map
c-on-time-with-unordered_map-by-lzl12463-dnk0
See my latest update in repo LeetCode\n\n\n// OJ: https://leetcode.com/problems/k-diff-pairs-in-an-array\n// Author: github.com/lzl124631x\n// Time: O(N)\n// Sp
lzl124631x
NORMAL
2017-03-06T12:55:01.121000+00:00
2022-02-09T09:07:52.544557+00:00
10,531
false
See my latest update in repo [LeetCode](https://github.com/lzl124631x/LeetCode)\n\n```\n// OJ: https://leetcode.com/problems/k-diff-pairs-in-an-array\n// Author: github.com/lzl124631x\n// Time: O(N)\n// Space: O(N)\nclass Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n if (k < 0) return 0;\n un...
63
1
[]
16
k-diff-pairs-in-an-array
[Python] O(n) solution, explained
python-on-solution-explained-by-dbabiche-gpcx
Let us just use counter and count frequency of each number in our array. We can have two options:\n\n1. k > 0, it means, that for each unique number i we are as
dbabichev
NORMAL
2020-10-03T07:28:58.578774+00:00
2020-10-03T07:28:58.578809+00:00
3,777
false
Let us just use counter and count frequency of each number in our array. We can have two options:\n\n1. `k > 0`, it means, that for each unique number `i` we are asking if number `i+k` also in table.\n2. `k = 0`, it means, that we are looking for pairs of equal numbers, so just check each frequency.\n\n**Complexity**: ...
62
9
[]
5
k-diff-pairs-in-an-array
1-liner in Python, O(n) time
1-liner-in-python-on-time-by-o_sharp-purd
\n def findPairs(self, nums, k):\n return len(set(nums)&{n+k for n in nums}) if k>0 else sum(v>1 for v in collections.Counter(nums).values()) if k==0 e
o_sharp
NORMAL
2017-03-05T10:04:11.950000+00:00
2018-10-01T02:19:30.865920+00:00
18,067
false
```\n def findPairs(self, nums, k):\n return len(set(nums)&{n+k for n in nums}) if k>0 else sum(v>1 for v in collections.Counter(nums).values()) if k==0 else 0\n```\nwhich is equivalent to:\n```\n def findPairs(self, nums, k):\n if k>0:\n return len(set(nums)&set(n+k for n in nums))\n ...
61
9
[]
17
k-diff-pairs-in-an-array
[C++] [Java] Clean Code with Explanation [set] [map]
c-java-clean-code-with-explanation-set-m-7su3
C++\n\nclass Solution {\npublic:\n /**\n * for every number in the array:\n * - if there was a number previously k-diff with it, save the smaller to
alexander
NORMAL
2017-03-05T04:02:31.343000+00:00
2018-10-05T03:12:11.511345+00:00
18,832
false
**C++**\n```\nclass Solution {\npublic:\n /**\n * for every number in the array:\n * - if there was a number previously k-diff with it, save the smaller to a set;\n * - and save the value-index to a map;\n */\n int findPairs(vector<int>& nums, int k) {\n if (k < 0) {\n return 0...
56
1
[]
10
k-diff-pairs-in-an-array
✔️ Python O(n) Solution | 98% Faster | Easy Solution | K-diff Pairs in an Array
python-on-solution-98-faster-easy-soluti-sqtp
\uD83D\uDD3C IF YOU FIND THIS POST HELPFUL PLEASE UPVOTE \uD83D\uDC4D\n\nVisit this blog to learn Python tips and techniques and to find a Leetcode solution wit
pniraj657
NORMAL
2022-02-09T06:34:27.713867+00:00
2023-02-01T05:42:24.115343+00:00
6,578
false
**\uD83D\uDD3C IF YOU FIND THIS POST HELPFUL PLEASE UPVOTE \uD83D\uDC4D**\n\nVisit this blog to learn Python tips and techniques and to find a Leetcode solution with an explanation: https://www.python-techs.com/\n\n**Solution:**\n```\nclass Solution:\n def findPairs(self, nums: List[int], k: int) -> int:\n cn...
39
3
['Python', 'Python3']
5
k-diff-pairs-in-an-array
C++ super-simple solution O(n), faster than 100%
c-super-simple-solution-on-faster-than-1-v4qf
\nclass Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n unordered_map<int, int> map;\n for(auto num:nums)\n map[nu
yehudisk
NORMAL
2020-10-03T18:42:45.002534+00:00
2020-10-03T18:42:45.002576+00:00
3,279
false
```\nclass Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n unordered_map<int, int> map;\n for(auto num:nums)\n map[num]++;\n \n int res = 0;\n if (k > 0) {\n for(auto a:map)\n if (map.find(a.first+k) != map.end()) \n ...
31
3
['C']
6
k-diff-pairs-in-an-array
Java | Easy to understand |Two Approaches | Sorting | HashMap
java-easy-to-understand-two-approaches-s-qznm
Method 1: using sorting and two pointers\n\nclass Solution {\n public int findPairs(int[] nums, int k) {\n Arrays.sort(nums);\n int i=0, j=1,
cyrus18
NORMAL
2022-02-09T08:20:51.996610+00:00
2022-02-09T08:20:51.996643+00:00
3,967
false
#### **Method 1: using sorting and two pointers**\n```\nclass Solution {\n public int findPairs(int[] nums, int k) {\n Arrays.sort(nums);\n int i=0, j=1, diff=0, n=nums.length, sum=Integer.MIN_VALUE;\n int count=0;\n while(j<n && i<n-1){\n\t\t // ((nums[i]+nums[j])!=sum) -> this will ...
27
0
['Two Pointers', 'Sorting', 'Binary Tree', 'Java']
1
k-diff-pairs-in-an-array
Interesting Java Solution/ HashSet Only
interesting-java-solution-hashset-only-b-8vay
Put all numbers n in Hashset S1.\nPut all numbers n+k in HashSet S2.\nThe number of pairs are the intersection of the two Hashsets. Different conditions apply t
wangdi814
NORMAL
2017-03-05T15:04:08.049000+00:00
2018-10-07T22:09:58.234090+00:00
4,270
false
Put all numbers n in Hashset S1.\nPut all numbers n+k in HashSet S2.\nThe number of pairs are the intersection of the two Hashsets. Different conditions apply to k=0 or k<0.\n\n```\npublic class Solution {\n public int findPairs(int[] nums, int k) {\n \n int ans = 0;\n \n if(k<0) return a...
24
0
[]
7
k-diff-pairs-in-an-array
[C++] Solution w/ Explanation| Brute force to optimize | Two approaches
c-solution-w-explanation-brute-force-to-7sfxr
Brief note about Question-\n\nWe have to return the number of unique k-diff pairs in the array.\n\nK- diff pair (arr[i], arr[j]) is nothing but basically \n 0
aryanttripathi
NORMAL
2022-02-09T06:37:01.306015+00:00
2024-08-31T11:25:51.174611+00:00
1,639
false
***Brief note about Question-***\n\nWe have to ***return the number of unique k-diff pairs in the array.***\n\nK- diff pair (arr[i], arr[j]) is nothing but basically \n* 0 < i < j < arr.size()\n* abs(arr[i] - arr[j]) == k\n______________\n***Solution - I (Accepted)-***\n* We try to implement what the question wants to...
19
0
['C']
1
k-diff-pairs-in-an-array
Readable & Simple Python
readable-simple-python-by-terribleprogra-ihxr
O(n) Time.\nO(n) Space.\n\nfrom collections import Counter\nclass Solution:\n def findPairs(self, nums: List[int], k: int) -> int:\n\t\t#If k is less than 0,
terribleprogrammer
NORMAL
2019-07-02T00:47:22.378651+00:00
2019-07-02T00:47:22.378691+00:00
3,180
false
O(n) Time.\nO(n) Space.\n```\nfrom collections import Counter\nclass Solution:\n def findPairs(self, nums: List[int], k: int) -> int:\n\t\t#If k is less than 0, then the result is 0 since we are looking fpr pairs with an ABSOLUTE difference of k.\n if k < 0:\n return 0\n \n count = Co...
19
0
['Ordered Set', 'Python']
3
k-diff-pairs-in-an-array
O(n) concise solution, C++
on-concise-solution-c-by-vsmnv-8npa
\nclass Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n if(k < 0) return 0;\n unordered_map<int,int> m;\n for(int i =
vsmnv
NORMAL
2017-03-05T04:35:57.988000+00:00
2017-03-05T04:35:57.988000+00:00
7,106
false
```\nclass Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n if(k < 0) return 0;\n unordered_map<int,int> m;\n for(int i = 0; i < nums.size(); ++i)\n ++m[nums[i]];\n int res = 0;\n if(k != 0) {\n for(auto it = m.begin(); it != m.end(); ++it)\n ...
19
0
['C++']
5
k-diff-pairs-in-an-array
[C++/Java/Python] Counter - O(N) - Clean & Concise
cjavapython-counter-on-clean-concise-by-8m5gh
Idea\n- Build cnt is a map to map unique numbers and their counts .\n- For each b in cnt: \n\t- If k > 0 and a = b - k exists then we count (a, b) as a k-diff p
hiepit
NORMAL
2020-10-03T09:14:01.410614+00:00
2020-10-03T09:47:43.956816+00:00
1,091
false
**Idea**\n- Build `cnt` is a map to map `unique numbers` and `their counts` .\n- For each `b` in `cnt`: \n\t- If `k > 0` and `a = b - k` exists then we count `(a, b)` as a `k-diff pair`.\n\t- If `k = 0` and `b` appears at least 2 times then we count`(b, b)` as a `k-diff pair`.\n\n**Complexity** \n- Time & Space: O(N)\n...
17
7
[]
1
k-diff-pairs-in-an-array
Java 6ms, Beats 98% Simple 2-Pointer Approach
java-6ms-beats-98-simple-2-pointer-appro-wtve
This is a pretty standard 2 pointer approach with a unique take away when looking at duplicates. One thing that took me a little bit to wrap my head around was
kniffina
NORMAL
2019-05-24T21:39:05.318382+00:00
2019-08-31T14:56:49.918655+00:00
3,012
false
This is a pretty standard 2 pointer approach with a unique take away when looking at duplicates. One thing that took me a little bit to wrap my head around was how we can accurately determine what is a valid answer. I also didn\'t see any posts where they showed this approach so I thought I would share.\n\n\nTo start I...
17
1
['Two Pointers', 'Java']
2
k-diff-pairs-in-an-array
Java O(n) Hashmap One Pass Easy Solution!!
java-on-hashmap-one-pass-easy-solution-b-1g1h
\npublic int findPairs(int[] nums, int k) {\n int count =0;\n HashMap<Integer, Integer> map = new HashMap<>();\n \n for(int i = 0; i
gargeemukherjee
NORMAL
2021-06-21T13:09:18.675032+00:00
2021-06-21T13:09:18.675084+00:00
1,650
false
```\npublic int findPairs(int[] nums, int k) {\n int count =0;\n HashMap<Integer, Integer> map = new HashMap<>();\n \n for(int i = 0; i < nums.length; i++) {\n if(!map.containsKey(nums[i])) {\n if(map.containsKey(nums[i] + k)) count++;\n if(map.contai...
16
1
['Java']
1
k-diff-pairs-in-an-array
Python concise O(N) solution using sets, only one pass through the list
python-concise-on-solution-using-sets-on-pcpz
Check whether num + k and num - k are already in the set and also make sure the pair is not already counted. Only goes throught the list one time.\n\nEdit: afte
bindloss
NORMAL
2019-10-07T20:55:18.545204+00:00
2019-10-09T03:32:43.280559+00:00
2,313
false
Check whether num + k and num - k are already in the set and also make sure the pair is not already counted. Only goes throught the list one time.\n\nEdit: after reading other solutions, some memory can be saved by only saving the smallest value between num1 and num2 in pairsSet instead of the sorted tuple.\n```\nclass...
16
0
['Python']
0
k-diff-pairs-in-an-array
Self-explained AC Java Sliding Window
self-explained-ac-java-sliding-window-by-mmn2
\n public int findPairs(int[] nums, int k) {\n\tif(k<0 || nums.length<=1){\n\t return 0;\n\t}\n\t\t \n Arrays.sort(nums);\n int count = 0;\n
2010zhouyang
NORMAL
2017-03-05T04:03:09.438000+00:00
2018-09-17T12:12:02.593212+00:00
2,888
false
```\n public int findPairs(int[] nums, int k) {\n\tif(k<0 || nums.length<=1){\n\t return 0;\n\t}\n\t\t \n Arrays.sort(nums);\n int count = 0;\n int left = 0;\n int right = 1;\n \n while(right<nums.length){\n int firNum = nums[left];\n int secNu...
15
1
[]
5
k-diff-pairs-in-an-array
Best Approach || Easy solution || easy-understanding
best-approach-easy-solution-easy-underst-gwtm
\n//Please upvote,if u like it :)\nint findPairs(vector<int>& nums, int k){\n unordered_map<int,int> mp;\n for(auto it:nums){\n mp[it]+
123_tripathi
NORMAL
2022-02-09T07:38:47.570768+00:00
2022-06-17T06:20:55.680187+00:00
2,636
false
```\n//Please upvote,if u like it :)\nint findPairs(vector<int>& nums, int k){\n unordered_map<int,int> mp;\n for(auto it:nums){\n mp[it]++;\n }\n int ans = 0;\n for(auto it:mp){\n int findd = it.first + k;\n if(mp.find(findd) != mp.end()){\n ...
13
1
['C', 'Ordered Set', 'Python', 'C++', 'Java']
1
k-diff-pairs-in-an-array
problem with description
problem-with-description-by-endurance-4heu
The question that is being checked for is not the question that is being asked.\n\nThe question being checked for seems to be:\n\nGiven an array of integers num
endurance
NORMAL
2022-02-09T02:42:19.620304+00:00
2022-02-09T06:53:20.816494+00:00
439
false
The question that is being checked for is not the question that is being asked.\n\nThe question being checked for seems to be:\n\n*Given an array of integers nums and an integer k, return the number of unique pairs that are of the form (nums[i], nums[j]) where*\n**nums[i] <= nums[j]\ni != j\nabs(nums[i] - nums[j]) == k...
11
0
[]
3
k-diff-pairs-in-an-array
Python 3, Faster than 99.77%, Dictionary
python-3-faster-than-9977-dictionary-by-qc60h
\n# Faster than 99.77% of Python3 online submissions\n# Memory Usage: 15.6 MB, less than 34.48% of Python3 online submissions\n\nclass Solution:\n def findPa
silvia42
NORMAL
2020-10-03T18:25:03.726621+00:00
2020-10-03T18:25:03.726667+00:00
2,090
false
```\n# Faster than 99.77% of Python3 online submissions\n# Memory Usage: 15.6 MB, less than 34.48% of Python3 online submissions\n\nclass Solution:\n def findPairs(self, nums: List[int], k: int) -> int:\n # return the number of unique pairs\n answ=0\n # create a dictionary: d[x]==nums.count(x)\n...
11
0
['Python', 'Python3']
1
k-diff-pairs-in-an-array
C++ O(nlogn) solution without hashmap [detail explanation]
c-onlogn-solution-without-hashmap-detail-uz3m
Sort and then perform two scans in parallel, maintaining a difference as close to k as possible between the two scan positions. In other words, advance the lead
jasperjoe
NORMAL
2020-02-22T02:20:46.590335+00:00
2020-02-22T02:21:20.471520+00:00
1,554
false
Sort and then perform two scans in parallel, maintaining a difference as close to k as possible between the two scan positions. In other words, advance the leading scan when the difference is smaller than k, and advance the lagging scan when the difference is greater. This way we either find a pair or scan through the ...
11
1
['C', 'Sorting', 'C++']
1
k-diff-pairs-in-an-array
Beats 99% of Python3(hashmap, no libraries)
beats-99-of-python3hashmap-no-libraries-x9kdh
\nclass Solution:\n def findPairs(self, nums: List[int], k: int) -> int:\n d = {}\n cnt = 0\n for i in nums:\n if i not in d:
dxc7528
NORMAL
2020-10-03T12:25:58.727970+00:00
2020-10-03T12:31:58.050217+00:00
457
false
```\nclass Solution:\n def findPairs(self, nums: List[int], k: int) -> int:\n d = {}\n cnt = 0\n for i in nums:\n if i not in d:\n d[i] = 1\n else:\n d[i] += 1\n \n if k == 0:\n for i in d.values():\n if ...
10
1
['Python']
2
k-diff-pairs-in-an-array
Java HashMap
java-hashmap-by-hobiter-wp4m
\n public int findPairs(int[] nums, int k) {\n if (k < 0) return 0;\n Map<Integer, Integer> map = new HashMap<>();\n int res = 0;\n
hobiter
NORMAL
2020-06-07T22:23:42.715418+00:00
2020-06-07T22:23:42.715452+00:00
825
false
```\n public int findPairs(int[] nums, int k) {\n if (k < 0) return 0;\n Map<Integer, Integer> map = new HashMap<>();\n int res = 0;\n for(int i : nums) {\n if (map.containsKey(i)) {\n if (k == 0 && map.get(i) == 1) {\n map.put(i, map.get(i) + ...
10
0
[]
0
k-diff-pairs-in-an-array
JavaScript Solution O(n) Beats 92%R and 100%M using map, easy to understand
javascript-solution-on-beats-92r-and-100-14y5
\t/*\n\t * @param {number[]} nums\n\t * @param {number} k\n\t * @return {number}\n\t /\n\tvar findPairs = function(nums, k) {\n\t\tif(nums.length === 0 || k < 0
congweibai
NORMAL
2019-10-28T11:00:53.224978+00:00
2019-10-28T11:00:53.225010+00:00
1,671
false
\t/**\n\t * @param {number[]} nums\n\t * @param {number} k\n\t * @return {number}\n\t */\n\tvar findPairs = function(nums, k) {\n\t\tif(nums.length === 0 || k < 0) return 0\n\t\tlet myMap = new Map(),\n\t\t\tcount = 0\n\t\t//Get wordcount\n\t\tfor(num of nums){\n\t\t\tmyMap.set(num,(myMap.get(num)+1) || 1)\n\t\t}\n\t\t...
10
0
['JavaScript']
1
k-diff-pairs-in-an-array
C++ easy solution
c-easy-solution-by-avinash1320-jh65
\nclass Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n map<pair<int, int>, int> m;\n for(int i=0;i<nums.size();i++){\n
avinash1320
NORMAL
2022-02-09T05:08:03.965097+00:00
2022-02-09T05:08:03.965125+00:00
1,117
false
```\nclass Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n map<pair<int, int>, int> m;\n for(int i=0;i<nums.size();i++){\n for(int j=i+1;j<nums.size();j++){\n if(abs(nums[i]-nums[j])==k and m.find({nums[j], nums[i]})==m.end())\n m[{nums[i],...
9
1
['C', 'C++']
1
k-diff-pairs-in-an-array
✅Multiple solutions in C++ with explanations!
multiple-solutions-in-c-with-explanation-4fms
\n> If you\u2019re interested in coding you can join my Discord Server, link in the comment section. Also if you find any mistake please let me know. Thank you!
dhruba-datta
NORMAL
2022-01-11T11:01:25.374916+00:00
2022-01-22T17:33:45.878280+00:00
913
false
\n> **If you\u2019re interested in coding you can join my Discord Server, link in the comment section. Also if you find any mistake please let me know. Thank you!\u2764\uFE0F**\n> \n\n---\n\n## Explanation:\n\n### Solution 01\n\n- Here we sort the vector to avoid duplicate elements in the set.\n- Take to loops & if the...
9
0
['C', 'Ordered Set', 'C++']
2
k-diff-pairs-in-an-array
C++ simple soln || Hashing || Beats - 99%, 99%
c-simple-soln-hashing-beats-99-99-by-hel-sit2
```\nclass Solution {\npublic:\n int findPairs(vector& nums, int k) {\n unordered_map m;\n for(int x: nums) m[x]++;\n int ans=0;\n
helixpranay31399
NORMAL
2020-10-03T18:21:37.907320+00:00
2020-10-09T07:49:53.146745+00:00
1,014
false
```\nclass Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n unordered_map<int,int> m;\n for(int x: nums) m[x]++;\n int ans=0;\n if(k==0)\n {\n for(auto x: m) if(x.second>1) ans++;\n }\n else\n {\n for(auto x: m)\n ...
9
0
['C', 'C++']
0
k-diff-pairs-in-an-array
JAVA - HashMap Solution / Easy to Understand
java-hashmap-solution-easy-to-understand-h5a9
\npublic int findPairs(int[] nums, int k) {\n\tif(nums == null || nums.length == 0 || k<0) return 0;\n\tint count = 0;\n\tHashMap<Integer, Integer> hm = new Has
anubhavjindal
NORMAL
2019-11-17T05:58:24.303783+00:00
2019-11-17T05:58:24.303828+00:00
781
false
```\npublic int findPairs(int[] nums, int k) {\n\tif(nums == null || nums.length == 0 || k<0) return 0;\n\tint count = 0;\n\tHashMap<Integer, Integer> hm = new HashMap<>();\n\tfor(int num : nums) \n\t\thm.put(num, hm.getOrDefault(num, 0)+1);\n\tfor(Map.Entry<Integer, Integer> e : hm.entrySet())\n\t\tif(k==0 && e.getVal...
9
0
[]
0
k-diff-pairs-in-an-array
Java, O(n), sets, clean, 7 ms
java-on-sets-clean-7-ms-by-gthor10-bum1
This problem has two main cases - when k == 0 and all others. The idea is - for number n if there is n + k in the array - pair is possible. We add all numbers f
gthor10
NORMAL
2019-09-16T03:33:24.765518+00:00
2019-09-16T03:33:24.765565+00:00
1,367
false
This problem has two main cases - when k == 0 and all others. The idea is - for number n if there is n + k in the array - pair is possible. We add all numbers from array to the set, then check for n + k. For k == 0 we need to count how many unqiue numbers repeated 2+ times. For that I use second set - add number that w...
9
0
['Java']
2
k-diff-pairs-in-an-array
Using HashMap | Java Code with explaination
using-hashmap-java-code-with-explainatio-faem
If you find it useful do upvote\n\nclass Solution {\n public int findPairs(int[] nums, int k) {\n \n Map<Integer, Integer> map = new HashMap<>(
rizon__kumar
NORMAL
2022-02-09T05:44:57.685225+00:00
2022-02-09T05:45:21.283112+00:00
397
false
If you find it useful do upvote\n```\nclass Solution {\n public int findPairs(int[] nums, int k) {\n \n Map<Integer, Integer> map = new HashMap<>();\n for(int i = 0; i < nums.length; i++){\n if(map.containsKey(nums[i])){\n // the array element and increament the freque...
8
0
[]
1
k-diff-pairs-in-an-array
C++ O(n) one pass with unordered_map
c-on-one-pass-with-unordered_map-by-jell-dgfo
```\nint findPairs(vector& nums, int k) {\n unordered_map mp;\n int ans = 0;\n if(k < 0) return 0;\n for(int num:nums){\n
jellyzhang
NORMAL
2020-02-24T09:34:58.607799+00:00
2020-02-24T09:35:15.462376+00:00
568
false
```\nint findPairs(vector<int>& nums, int k) {\n unordered_map<int,int> mp;\n int ans = 0;\n if(k < 0) return 0;\n for(int num:nums){\n if(k == 0 && mp[num] == 1){\n ans++;\n }else if(k > 0 && mp[num] == 0){\n ans += mp.count(num - k) + mp....
8
0
['C++']
1
k-diff-pairs-in-an-array
Java two pointer solution beats 97%
java-two-pointer-solution-beats-97-by-no-zqtv
The idea is simple. Sort the array first. Then for each number in the array, find if there exists a number satisfy the requirement. Note that right = Math.max(r
notarealname
NORMAL
2017-04-06T05:31:05.548000+00:00
2017-04-06T05:31:05.548000+00:00
1,156
false
The idea is simple. Sort the array first. Then for each number in the array, find if there exists a number satisfy the requirement. Note that ```right = Math.max(right, i + 1)``` can make sure each number in the array is accessed at most twice. So the time complexity is O(nlogn) + O(n) = O(nlogn)\n```\npublic int findP...
8
0
[]
1
k-diff-pairs-in-an-array
Easy | Commented | JavaScript hashmap | O(n) solution
easy-commented-javascript-hashmap-on-sol-ecjo
\nvar findPairs = function (nums, k) {\n\tlet map = {}, //Object to store count/frequency of numbers in array\n\t\tcount = 0; //count the desired output/result\
_kapi1
NORMAL
2022-02-09T03:35:24.413155+00:00
2022-02-09T03:35:53.126643+00:00
1,086
false
```\nvar findPairs = function (nums, k) {\n\tlet map = {}, //Object to store count/frequency of numbers in array\n\t\tcount = 0; //count the desired output/result\n\n\t//loop through the array and store the count/frequency in the object\n\tfor (let i = 0; i < nums.length; i++) {\n\t\t/*if num appears for the 1st time t...
7
0
['JavaScript']
0
k-diff-pairs-in-an-array
Simple java O(n) solution with explaination
simple-java-on-solution-with-explainatio-ct7b
\npublic int findPairs(int[] nums, int k) {\n HashMap<Integer,Integer> map = new HashMap<>();\n int res=0;\n //storing count of all the ele
sameep0108
NORMAL
2020-10-08T04:24:13.223834+00:00
2020-10-08T04:24:13.223863+00:00
1,087
false
``` \npublic int findPairs(int[] nums, int k) {\n HashMap<Integer,Integer> map = new HashMap<>();\n int res=0;\n //storing count of all the elements\n for(int i:nums){\n map.put(i,map.getOrDefault(i,0)+1);\n }\n for(int a:map.keySet()){\n if(k!=0){\n ...
7
0
['Java']
1
k-diff-pairs-in-an-array
K diff Pairs | C++ 6 liner solution with explanation | O(nlogn) time | O(1) space
k-diff-pairs-c-6-liner-solution-with-exp-za0h
Upvote this post, if you liked it. Happy Coding :)\n\nApproach : \n1) Sort the nums array.\n2) Loop over the nums array using iterator\n\ta) maintain the previ
sarthakjain1147
NORMAL
2020-10-03T16:55:11.650836+00:00
2020-10-03T18:35:19.230574+00:00
471
false
<b> Upvote this post, if you liked it. Happy Coding :)</b>\n\nApproach : \n1) Sort the nums array.\n2) Loop over the nums array using iterator\n\ta) maintain the previous value so that we can jump over duplicate values, to make sure that only unique pairs will be counted.\n\tb) perform binary search on elements from th...
7
1
[]
3
k-diff-pairs-in-an-array
C++ Solution | One-Pass | Beats 100% | Two-Pointer Approach
c-solution-one-pass-beats-100-two-pointe-so3k
Sort the vector.\nHere 3 cases arise : \n Case - 1 : When nums[j] - nums[i] > k\n\tThe difference between element at j and element at i is greater than required
pikabu
NORMAL
2020-10-03T09:43:52.626523+00:00
2020-10-03T09:43:52.626554+00:00
751
false
Sort the vector.\nHere 3 cases arise : \n* **Case - 1 : When nums[j] - nums[i] > k**\n\tThe difference between element at j and element at i is greater than required, so to reduce it increment i.\n* **Case - 2 : When nums[j] - nums[i] < k**\n\tThe difference between element at j and element at i is lesser than required...
7
1
['C']
3
k-diff-pairs-in-an-array
Easy to understand 2 pointer / sliding window approach in python O(1) space, O(nlogn) time
easy-to-understand-2-pointer-sliding-win-dcds
\n def findPairs(self, nums, k):\n """\n :type nums: List[int]\n :type k: int\n :rtype: int\n """\n count = 0\n
leetcoder89
NORMAL
2018-07-16T00:43:01.344647+00:00
2018-07-16T00:43:01.344647+00:00
672
false
```\n def findPairs(self, nums, k):\n """\n :type nums: List[int]\n :type k: int\n :rtype: int\n """\n count = 0\n nums.sort()\n \n slow = 0\n fast = 1\n size = len(nums)\n \n while fast < size:\n if nums[fast] - nu...
7
0
[]
0
k-diff-pairs-in-an-array
Simple Java O(n) with single For-loop & single HashMap
simple-java-on-with-single-for-loop-sing-7xth
Solved it by One For-loop and One HashMap\n```\npublic int findPairs(int[] nums, int k) {\n if(k < 0) return 0;\n Map map = new HashMap();\n int ret =
luckman
NORMAL
2017-04-24T23:56:32.812000+00:00
2018-09-30T00:13:37.001005+00:00
1,364
false
Solved it by One For-loop and One HashMap\n```\npublic int findPairs(int[] nums, int k) {\n if(k < 0) return 0;\n Map<Integer, Boolean> map = new HashMap<Integer, Boolean>();\n int ret = 0;\n for(int n : nums){\n /* if smaller matched value exists */\n if(map.containsKey(n-k) && !map.get(n-k))...
7
0
[]
0
k-diff-pairs-in-an-array
c++ using map easy soln
c-using-map-easy-soln-by-klaus04-i0um
\nclass Solution {\npublic:\n int findPairs(vector& nums, int k) {\n \n int n = nums.size();\n mapm; \n for(int i = 0;i1){\
klaus04
NORMAL
2022-02-09T05:13:50.798632+00:00
2022-02-09T05:13:50.798681+00:00
117
false
\nclass Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n \n int n = nums.size();\n map<int,int>m; \n for(int i = 0;i<n;i++)\n {\n m[nums[i]]++;\n }\n int sum = 0; \n if(k == 0){\n for(auto i:m){\n if(i...
6
0
[]
0
k-diff-pairs-in-an-array
Python O(n) solution using hashmap
python-on-solution-using-hashmap-by-bth3-w2sa
```\nclass Solution:\n def findPairs(self, nums: List[int], k: int) -> int:\n \n hashNums={}\n pairs=set()\n \n for n in n
BTh3
NORMAL
2022-02-09T00:33:23.098876+00:00
2022-02-09T00:33:23.098908+00:00
676
false
```\nclass Solution:\n def findPairs(self, nums: List[int], k: int) -> int:\n \n hashNums={}\n pairs=set()\n \n for n in nums: # O(n)\n hashNums[n]=hashNums.get(n,0)+1\n \n if n-k in hashNums:\n pairs.add(tuple(set([n,n-k])))\n ...
6
1
[]
0
k-diff-pairs-in-an-array
c++ || O(n) - hashing
c-on-hashing-by-wienczyslaw-jtq9
```\nclass Solution {\npublic:\n int findPairs(vector& nums, int k) {\n \n unordered_map hash;\n for(int i=0;i1) \n c
wienczyslaw
NORMAL
2022-02-05T16:13:52.931817+00:00
2022-02-05T16:13:52.931853+00:00
568
false
```\nclass Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n \n unordered_map<int,int> hash;\n for(int i=0;i<nums.size();i++) hash[nums[i]]++;\n \n int count =0; \n \n for(auto i: hash){\n if(k==0) {\n if(i.second>1) \n ...
6
0
['C', 'C++']
0
k-diff-pairs-in-an-array
Python faster than 93%
python-faster-than-93-by-dh7-ain7
\nclass Solution:\n def findPairs(self, a: List[int], K: int) -> int:\n s = set(a)\n if K == 0: return sum(1 for x in s if a.count(x) > 1)\n
dh7
NORMAL
2020-10-28T20:57:57.413581+00:00
2020-10-28T20:57:57.413616+00:00
645
false
```\nclass Solution:\n def findPairs(self, a: List[int], K: int) -> int:\n s = set(a)\n if K == 0: return sum(1 for x in s if a.count(x) > 1)\n return sum(1 for x in s if x + K in s)\n```
6
2
['Python']
1
k-diff-pairs-in-an-array
C : 4-lines - simple n short - no built-in Utils/Data Structures, etc
c-4-lines-simple-n-short-no-built-in-uti-l53j
\nint findPairs(int* nums, int numsSize, int k){\n char a[200000] = { 0 }, *m = &a[500];\n for (int i = 0, c = 0, *n = nums ; i < numsSize || (numsSize =
universalcoder12
NORMAL
2020-08-27T16:54:04.912456+00:00
2020-08-27T21:45:12.268952+00:00
355
false
```\nint findPairs(int* nums, int numsSize, int k){\n char a[200000] = { 0 }, *m = &a[500];\n for (int i = 0, c = 0, *n = nums ; i < numsSize || (numsSize = c, 0) ; i++)\n for (int j = i, p ; ++j < numsSize ; abs(n[i] - n[j]) == k && !m[p = n[i] + n[j]] ? c += m[p] = 1 : 0);\n return numsSize;\n}\n```
6
1
['C']
0
k-diff-pairs-in-an-array
532: Solution with step by step explanation
532-solution-with-step-by-step-explanati-zy6t
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n1. First, check if k is less than 0, if yes then return 0.\n2. Create a h
Marlen09
NORMAL
2023-03-13T16:49:21.050159+00:00
2023-03-13T16:49:21.050201+00:00
1,941
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. First, check if k is less than 0, if yes then return 0.\n2. Create a hash table freq to store the frequency of each number in the array.\n3. Iterate through the array nums and for each number, update its frequency in the ...
5
0
['Array', 'Hash Table', 'Two Pointers', 'Python', 'Python3']
1
k-diff-pairs-in-an-array
Python 7-line Super Simple and Short Solution
python-7-line-super-simple-and-short-sol-bqk8
\nclass Solution:\n def findPairs(self, nums: List[int], k: int) -> int:\n if (k == 0):\n return len([item for item, count in collections.C
yehudisk
NORMAL
2021-01-05T19:22:17.183198+00:00
2021-01-05T19:22:17.183243+00:00
466
false
```\nclass Solution:\n def findPairs(self, nums: List[int], k: int) -> int:\n if (k == 0):\n return len([item for item, count in collections.Counter(nums).items() if count > 1])\n \n nums = set(nums)\n res = 0\n for n in nums:\n res += 1 if n-k in nums else 0\...
5
1
['Python']
0
k-diff-pairs-in-an-array
Java O(n) solution with hashmap
java-on-solution-with-hashmap-by-user505-vnw9
\nclass Solution {\n public int findPairs(int[] nums, int k) {\n int kDiffs = 0;\n Map<Integer, Integer> counter = new HashMap<>();\n \n
user5055f
NORMAL
2020-10-06T12:31:01.995429+00:00
2020-10-06T12:31:01.995474+00:00
323
false
```\nclass Solution {\n public int findPairs(int[] nums, int k) {\n int kDiffs = 0;\n Map<Integer, Integer> counter = new HashMap<>();\n \n for (int n: nums) {\n int countOfN = counter.getOrDefault(n, 0);\n countOfN++;\n counter.put(n, countOfN);\n ...
5
0
[]
2
k-diff-pairs-in-an-array
C++, self-explanatory one-pass O(n) use hash map
c-self-explanatory-one-pass-on-use-hash-lgefy
cpp\nint findPairs(vector<int>& nums, int k) {;\n unordered_map<int, int> freq;\n int ans = 0;\n for (int n : nums) {\n if (k == 0) {\n
alvin-777
NORMAL
2020-10-03T09:18:24.872048+00:00
2020-10-03T09:18:24.872081+00:00
532
false
```cpp\nint findPairs(vector<int>& nums, int k) {;\n unordered_map<int, int> freq;\n int ans = 0;\n for (int n : nums) {\n if (k == 0) {\n if (freq[n] == 1) ++ans; // only count once\n } else if (!freq.count(n)) { // only count when the first time n appears\n if (freq.count(...
5
0
['C']
1
k-diff-pairs-in-an-array
[Python] really short and easy, explained
python-really-short-and-easy-explained-b-7gq9
Let complementary number be a number that is equal to some given number minus k.\nTo form at least one pair for any given number:\n If k is greater than zero,
user9823
NORMAL
2020-10-03T08:09:18.559186+00:00
2020-10-03T09:50:44.827582+00:00
343
false
Let complementary number be a number that is equal to some given number minus k.\nTo form at least one pair for any given number:\n If k is greater than zero, we only care if count of complementary number is bigger than zero.\n If k is zero, we only care if count of complementary number (that is the current number it...
5
0
[]
1
k-diff-pairs-in-an-array
Java O(n) solution with comments
java-on-solution-with-comments-by-nishth-2l2i
class Solution {\n public int findPairs(int[] nums, int k) {\n \n int output=0;\n HashMap hm = new HashMap<>(); \n \n //Stor
nishtha_na
NORMAL
2020-08-16T11:44:21.715676+00:00
2020-08-16T11:47:35.747003+00:00
417
false
class Solution {\n public int findPairs(int[] nums, int k) {\n \n int output=0;\n HashMap <Integer,Integer> hm = new HashMap<>(); \n \n //Storing the frequencies in the hashmpap\n for(int num: nums)\n {\n hm.put(num, hm.getOrDefault(num,0)+1);\n }\n ...
5
0
[]
3
k-diff-pairs-in-an-array
Python, two pointer, O(nlogn)
python-two-pointer-onlogn-by-clarketm-27f6
\ndef find_pairs(L: List[int], k: int) -> int:\n """\n K-diff Pairs in an Array\n\n time: O(nlogn)\n space: O(1)\n\n :param List[int] L:\n :pa
clarketm
NORMAL
2020-04-18T08:06:51.451111+00:00
2020-04-18T08:07:43.491408+00:00
1,181
false
```\ndef find_pairs(L: List[int], k: int) -> int:\n """\n K-diff Pairs in an Array\n\n time: O(nlogn)\n space: O(1)\n\n :param List[int] L:\n :param int k:\n :return int:\n """\n L.sort()\n\n N = len(L)\n\n i = pairs = 0\n j = 1\n\n while j < N:\n if j < N - 1 and L[j] == L...
5
0
['Two Pointers', 'Python', 'Python3']
0
k-diff-pairs-in-an-array
JavaScript using object as map
javascript-using-object-as-map-by-shengd-i1qm
\n/**\n * @param {number[]} nums\n * @param {number} k\n * @return {number}\n */\nvar findPairs = function(nums, k) {\n if (!nums.length || k < 0) return 0;\n
shengdade
NORMAL
2020-02-03T21:25:16.473244+00:00
2020-02-03T21:25:16.473277+00:00
857
false
```\n/**\n * @param {number[]} nums\n * @param {number} k\n * @return {number}\n */\nvar findPairs = function(nums, k) {\n if (!nums.length || k < 0) return 0;\n const map = {};\n let counter = 0;\n nums.forEach(n => {\n map[n] = (map[n] || 0) + 1;\n });\n Object.keys(map).forEach(key => {\n if (k === 0) {\...
5
0
['JavaScript']
1
k-diff-pairs-in-an-array
3 java solutions: 1.HashMap without sort; 2.HashMap + sort; 3. Sort + two points
3-java-solutions-1hashmap-without-sort-2-63qt
HashMap without sort.\nWe define a hashMap. The key element denotes the members of the array. The value has 3 types:\nvalue 1: the key appears once in the array
miaoyao
NORMAL
2019-12-31T03:33:25.568948+00:00
2019-12-31T03:35:52.088854+00:00
1,012
false
1. HashMap without sort.\nWe define a hashMap. The key element denotes the members of the array. The value has 3 types:\nvalue 1: the key appears once in the array\nvalue 2: the key appears more than once in the array\nvalue 0: the key has been used, this value is used to avoid duplicated pairs.\nWe use a for-loop to i...
5
0
['Java']
0
k-diff-pairs-in-an-array
7 Lines O(n) Python3 (fast and clear and easy understand)
7-lines-on-python3-fast-and-clear-and-ea-9n7w
make a dictory,and then when k>0 and k==0, we add res :\npython\n def findPairs(self, nums, k):\n\t\tnums.sort()\n res , dic= 0 , {}\n for i in n
macqueen
NORMAL
2019-10-26T08:48:46.287054+00:00
2019-10-26T08:58:09.052157+00:00
633
false
make a dictory,and then when k>0 and k==0, we add res :\n```python\n def findPairs(self, nums, k):\n\t\tnums.sort()\n res , dic= 0 , {}\n for i in nums:\n dic[i] = dic[i]+1 if i in dic else 1\n for i in dic.keys():\n if (i+k in dic and k>0) or (k==0 and dic[i]>1):\n ...
5
0
['Python3']
1
k-diff-pairs-in-an-array
Easy to Understand Python Solution using hashmap
easy-to-understand-python-solution-using-hugf
\n def findPairs(self, nums, k):\n """\n :type nums: List[int]\n :type k: int\n :rtype: int\n """\n if k < 0:\n
pgonarkar
NORMAL
2019-01-30T01:57:53.961368+00:00
2019-01-30T01:57:53.961458+00:00
416
false
```\n def findPairs(self, nums, k):\n """\n :type nums: List[int]\n :type k: int\n :rtype: int\n """\n if k < 0:\n return 0\n hashmap = {}\n count = 0\n for num in nums:\n # check num + k and num - k present in hashmap\n ...
5
0
[]
0
k-diff-pairs-in-an-array
Simple Idea O(nlogn) time + O(1) space Java Solution
simple-idea-onlogn-time-o1-space-java-so-ih8m
guess this solution is intuitive.\n\npublic class Solution {\n public int findPairs(int[] nums, int k) {\n if (nums == null || nums.length < 2) return
shevchenko_7
NORMAL
2017-03-06T07:40:23.698000+00:00
2017-03-06T07:40:23.698000+00:00
844
false
guess this solution is intuitive.\n```\npublic class Solution {\n public int findPairs(int[] nums, int k) {\n if (nums == null || nums.length < 2) return 0;\n int res = 0;\n Arrays.sort(nums);\n for (int i = 0; i < nums.length; i++) {\n if (i > 0 && nums[i] == nums[i - 1]) cont...
5
0
[]
1
k-diff-pairs-in-an-array
Short Java Solution, but two HashSets
short-java-solution-but-two-hashsets-by-tgmxi
\npublic class Solution {\n public int findPairs(int[] nums, int k) {\n Arrays.sort(nums);\n Set<Integer> seenNum = new HashSet<>();\n S
shawngao
NORMAL
2017-03-05T04:15:53.132000+00:00
2018-08-19T03:13:39.474564+00:00
1,186
false
```\npublic class Solution {\n public int findPairs(int[] nums, int k) {\n Arrays.sort(nums);\n Set<Integer> seenNum = new HashSet<>();\n Set<String> seenPair = new HashSet<>();\n int result = 0;\n \n for (int i = 0; i < nums.length; i++) {\n int prev = nums[i] - ...
5
1
[]
1
k-diff-pairs-in-an-array
Fastest and Easiest Solution
fastest-and-easiest-solution-by-sbt1999-yhvp
\n# Approach\nInitialization:\n\nseen = {} (an empty HashSet to keep track of numbers we\'ve seen so far)\nuniquePairs = {} (an empty HashSet to store the first
Sbt1999
NORMAL
2024-05-25T06:12:45.696647+00:00
2024-05-25T06:12:45.696681+00:00
721
false
\n# Approach\nInitialization:\n\nseen = {} (an empty HashSet to keep track of numbers we\'ve seen so far)\nuniquePairs = {} (an empty HashSet to store the first element of each unique pair)\n\n**Iteration 1:**\n\nCurrent number: 3\n\n Check if 3 - 2 = 1 is in seen: false\n Check if 3 + 2 = 5 is in seen: false\n ...
4
0
['Java']
0
k-diff-pairs-in-an-array
Java || very easy || Explanation || Hashmap
java-very-easy-explanation-hashmap-by-ha-aywv
Intuition\nVery easy approach to solve the problem with the help of single loop and hashmap\n\n# Approach\n\n initiate a Map \n enter all elements and its frequ
harshverma2702
NORMAL
2023-03-22T18:59:58.490723+00:00
2023-03-22T18:59:58.490761+00:00
1,312
false
# Intuition\nVery easy approach to solve the problem with the help of single loop and hashmap\n\n# Approach\n\n* initiate a Map \n* enter all elements and its frequency\n* inside of entry loop of map ,check\n1. if k==0 , it means any element which is occuring more then twice will always have diff ==0 , ex- 1-1=0 , 15-1...
4
0
['Java']
0
k-diff-pairs-in-an-array
C++ Two-Pointer - T(NlogN) and HashMap - T(N)
c-two-pointer-tnlogn-and-hashmap-tn-by-a-syg7
TWO-POINTER APPROACH T(NLogN)\n\n1. The approach that I used here is similar to two-sum problem.\n2. In two-sum we select two numbers that sums-up to k, whereas
akshatchaturvedi17
NORMAL
2022-02-09T10:20:18.397971+00:00
2022-02-09T10:37:20.946878+00:00
511
false
**TWO-POINTER APPROACH T(NLogN)**\n\n1. The approach that I used here is similar to two-sum problem.\n2. In two-sum we select two numbers that sums-up to k, whereas here we have to select two numbers with difference k.\n3. I have used two pointers i & j, in two-sum the we start i from 0 and j from n-1, but here we\'ll ...
4
0
['Two Pointers', 'C']
0
k-diff-pairs-in-an-array
C++ || Simple & Easy Solution|| O(n) || Hashmap
c-simple-easy-solution-on-hashmap-by-nab-h2fa
\n\nclass Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n int count =0;\n unordered_map<int,int> mp;\n \n for(i
nabil_jahan
NORMAL
2022-02-09T04:49:28.364205+00:00
2022-03-21T19:53:46.632197+00:00
337
false
\n```\nclass Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n int count =0;\n unordered_map<int,int> mp;\n \n for(int i=0;i<nums.size();i++) mp[nums[i]]++; \n for(auto i: mp){\n if(k==0) {\n if(i.second>1) \n count++;\n ...
4
0
['C', 'C++']
0
k-diff-pairs-in-an-array
💚[C++] 95% Fast and Easy Solution Explained | HASHMAP
c-95-fast-and-easy-solution-explained-ha-7w39
Welcome to abivilion\'s solution. Kindly Upvote for supporting this article.\n\nSOLUTION\nTC - O(n)\nSC - O(N)\n\n\nclass Solution {\npublic:\n int findPairs
abivilion
NORMAL
2022-02-09T01:47:08.747598+00:00
2022-02-09T01:47:08.747630+00:00
555
false
**Welcome to abivilion\'s solution. Kindly Upvote for supporting this article.**\n\n**SOLUTION**\n**TC - O(n)**\n**SC - O(N)**\n\n```\nclass Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n \n unordered_map<int,int>st;\n int count=0;\n \n\t\t//storing in map\n for(...
4
0
['C', 'C++']
0
k-diff-pairs-in-an-array
Binary Search solution [Java]
binary-search-solution-java-by-vaidehi21-o8q0
\nclass Solution {\n public int findPairs(int[] nums, int k) {\n Arrays.sort(nums);\n int count=0;\n for(int i=0;i<nums.length;i++)\n
vaidehi21
NORMAL
2020-10-03T16:18:09.395133+00:00
2020-10-03T16:18:09.395166+00:00
403
false
```\nclass Solution {\n public int findPairs(int[] nums, int k) {\n Arrays.sort(nums);\n int count=0;\n for(int i=0;i<nums.length;i++)\n { \n int low=i+1;\n int high=nums.length-1;\n while(low<=high)\n {\n int mid=low+(high-low)/...
4
0
['Binary Tree', 'Java']
0
k-diff-pairs-in-an-array
Java Soln | 1 pass | O(N)- time and space
java-soln-1-pass-on-time-and-space-by-ap-qq2a
\nclass Solution {\n public int findPairs(int[] nums, int k) {\n HashSet<Integer> set = new HashSet<>();\n\t\tint count = 0;\n\t\t\n\t\tHashSet<Intege
apex2911
NORMAL
2020-10-03T10:29:49.626353+00:00
2020-10-03T10:30:10.279457+00:00
290
false
```\nclass Solution {\n public int findPairs(int[] nums, int k) {\n HashSet<Integer> set = new HashSet<>();\n\t\tint count = 0;\n\t\t\n\t\tHashSet<Integer> duplicateset = new HashSet<>();\n\t\t\n\t\t\n\t\tfor (int n : nums) {\n\t\t\tif (set.contains(n)) {\n\t\t\t\tif(k==0 && !duplicateset.contains(n)) {\n\t\t...
4
0
[]
0
k-diff-pairs-in-an-array
Easy JS Solution
easy-js-solution-by-hbjorbj-t4zp
\nvar findPairs = function(nums, k) {\n if (k < 0) return 0; \n nums = (k === 0) ? nums : Array.from(new Set(nums));\n let m = new Map(), res = 0;\n
hbjorbj
NORMAL
2020-09-05T13:17:24.019093+00:00
2020-09-05T14:18:44.919003+00:00
470
false
```\nvar findPairs = function(nums, k) {\n if (k < 0) return 0; \n nums = (k === 0) ? nums : Array.from(new Set(nums));\n let m = new Map(), res = 0;\n for (let num of nums) {\n if (m.get(num+k) === 1) res++;\n if (num+k !== num-k && m.get(num-k) === 1) res++;\n m.set(num, m.get(num)+...
4
0
['JavaScript']
1
k-diff-pairs-in-an-array
O(N), very easy way by using hashset
on-very-easy-way-by-using-hashset-by-che-5ay2
\tfrom typing import List\n\n\n\tclass Solution:\n\t\tdef findPairs(self, nums: List[int], k: int) -> int:\n\t\t\tif k < 0:\n\t\t\t\treturn 0\n\n\t\t\tsaw = set
chen_yiming
NORMAL
2020-05-28T13:53:54.959435+00:00
2020-05-28T13:53:54.959485+00:00
579
false
\tfrom typing import List\n\n\n\tclass Solution:\n\t\tdef findPairs(self, nums: List[int], k: int) -> int:\n\t\t\tif k < 0:\n\t\t\t\treturn 0\n\n\t\t\tsaw = set()\n\t\t\tpair = set()\n\n\t\t\tfor num in nums:\n\t\t\t\tif num + k in saw:\n\t\t\t\t\tpair.add((min(num + k, num), max(num + k, num)))\n\n\t\t\t\tif num - k i...
4
0
['Python3']
0
k-diff-pairs-in-an-array
python, reasonably fast (>99.4%), short and readable, explained, different options
python-reasonably-fast-994-short-and-rea-2vqz
Let\'s start with something easy to read:\n\n\nclass Solution:\n def findPairs(self, nums: List[int], k: int) -> int:\n r = 0\n if k >= 0:\n
rmoskalenko
NORMAL
2020-03-05T22:40:34.051529+00:00
2020-03-06T01:36:18.250310+00:00
499
false
Let\'s start with something easy to read:\n\n```\nclass Solution:\n def findPairs(self, nums: List[int], k: int) -> int:\n r = 0\n if k >= 0:\n c = collections.Counter(nums)\n for i in c:\n if (k==0 and c[i]>1) or (k!=0 and i+k in c):\n r += 1\n ...
4
0
[]
0
k-diff-pairs-in-an-array
Python solution
python-solution-by-rogerfederer-4cuc
\nclass Solution(object):\n def findPairs(self, nums, k):\n """\n :type nums: List[int]\n :type k: int\n :rtype: int\n """
rogerfederer
NORMAL
2018-07-04T18:30:02.858940+00:00
2018-07-04T18:30:02.858940+00:00
998
false
```\nclass Solution(object):\n def findPairs(self, nums, k):\n """\n :type nums: List[int]\n :type k: int\n :rtype: int\n """\n if k < 0:\n return 0\n res = 0\n counter = collections.Counter(nums)\n for key in counter:\n if k != 0:\...
4
1
[]
0
k-diff-pairs-in-an-array
✅3 Method || C++ || Beginner Friendly ||Beat 100%
3-method-c-beginner-friendly-beat-100-by-dhlh
To solve the problem of finding all unique pairs in an integer array where each pair has a specific difference \( k \), there are several approaches, each with
BhavayGupta2002
NORMAL
2024-10-30T12:26:31.758746+00:00
2024-10-30T12:26:31.758782+00:00
331
false
To solve the problem of finding all unique pairs in an integer array where each pair has a specific difference \\( k \\), there are several approaches, each with its own complexity and trade-offs. Here\u2019s an overview of the approaches, including the best one:\n\n### 1. **Brute Force Approach**\n- **Idea**: Check al...
3
1
['Array', 'Hash Table', 'Two Pointers', 'C++']
1
k-diff-pairs-in-an-array
Brute Force to Functional Programming | Multiple Approaches
brute-force-to-functional-programming-mu-13rx
Solution 1: Brute Force Approach\n\n### Intuition\n- The simplest approach is to check all possible pairs in the array and see if their absolute difference equa
lokeshwar777
NORMAL
2024-10-10T17:48:36.925572+00:00
2024-10-10T17:48:36.925605+00:00
265
false
## Solution 1: Brute Force Approach\n\n### Intuition\n- The simplest approach is to check all possible pairs in the array and see if their absolute difference equals $$( k $$).\n\n### Approach\n- Use two nested loops to iterate through the array, check the absolute difference for each pair, and store unique pairs in a ...
3
0
['Hash Table', 'Two Pointers', 'Sorting', 'Python3']
0
k-diff-pairs-in-an-array
✅ Beats 99.99% || Two Pointer || K-diff Pairs in an Array
beats-9999-two-pointer-k-diff-pairs-in-a-k9rp
Intuition\nThe code aims to find the number of unique pairs (i, j) in the given vector nums such that the absolute difference between nums[i] and nums[j] is equ
gouravsharma2806
NORMAL
2024-01-18T04:56:27.843452+00:00
2024-01-18T04:56:27.843493+00:00
1,586
false
# Intuition\nThe code aims to find the number of unique pairs (i, j) in the given vector nums such that the absolute difference between nums[i] and nums[j] is equal to the given integer k.\n\n# Approach\nSort the array nums to make it easier to find pairs with the desired difference.\nUse two pointers (i and j) to trav...
3
0
['Two Pointers', 'Binary Search', 'Sorting', 'C++']
0
k-diff-pairs-in-an-array
Unique Solution using Set Nlog(N)!!!
unique-solution-using-set-nlogn-by-aero_-rmg5
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
aero_coder
NORMAL
2023-08-01T09:04:15.957547+00:00
2023-09-05T07:19:29.504565+00:00
258
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
3
0
['C++']
1
k-diff-pairs-in-an-array
Using Binary Search
using-binary-search-by-sithkar-wflh
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n1. The input nums vecto
Sithkar
NORMAL
2023-07-29T10:36:14.581851+00:00
2023-07-29T10:36:14.581869+00:00
1,279
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. The input nums vector is sorted in ascending order, just like in the previous solution.\n\n2. The bs function is a binary search function that searches for the valu...
3
0
['Binary Search', 'C++']
0
k-diff-pairs-in-an-array
C++ Solution
c-solution-by-pranto1209-1a0o
Code\n\nclass Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n unordered_map<int, int> mp;\n for(int x: nums) mp[x]++;\n
pranto1209
NORMAL
2023-06-03T10:29:12.049017+00:00
2023-06-03T10:29:12.049058+00:00
194
false
# Code\n```\nclass Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n unordered_map<int, int> mp;\n for(int x: nums) mp[x]++;\n int ans = 0;\n for(auto x: mp) {\n if (k == 0) { \n if(x.second > 1) ans++;\n }\n else if (mp.f...
3
0
['C++']
0
k-diff-pairs-in-an-array
Easiest way using Java
easiest-way-using-java-by-us2463035-v6bq
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
us2463035
NORMAL
2023-05-01T07:10:22.069884+00:00
2023-05-01T07:10:22.069938+00:00
973
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
3
0
['Java']
1
k-diff-pairs-in-an-array
Easy C++ Solution using binary search
easy-c-solution-using-binary-search-by-h-qsqz
\n\n# Code\n\n//TC=O(nlogn)\nclass Solution {\npublic:\nint search(vector<int>& nums,int t,int s){\n int e=nums.size()-1;\n while(s<=e){\n int mid=
Harshit-Vashisth
NORMAL
2023-03-07T20:32:51.512973+00:00
2023-03-09T06:47:53.137342+00:00
855
false
\n\n# Code\n```\n//TC=O(nlogn)\nclass Solution {\npublic:\nint search(vector<int>& nums,int t,int s){\n int e=nums.size()-1;\n while(s<=e){\n int mid=s+(e-s)/2;\n if(nums[mid]==t)\n return mid;\n else if(t>nums[mid])\n s=mid+1;\n else\n e=mid-1;\n }\n return ...
3
0
['C++']
0
k-diff-pairs-in-an-array
Java | Hashmap
java-hashmap-by-aanchal002-h492
\nclass Solution {\n public int findPairs(int[] nums, int k) {\n HashMap<Integer,Integer> map=new HashMap<>();\n int c=0;\n for(int val:
Aanchal002
NORMAL
2022-04-07T17:16:30.987413+00:00
2022-04-07T17:16:30.987439+00:00
599
false
```\nclass Solution {\n public int findPairs(int[] nums, int k) {\n HashMap<Integer,Integer> map=new HashMap<>();\n int c=0;\n for(int val:nums )\n {\n map.put(val,map.getOrDefault(val,0)+1);\n \n }\n for(int i:map.keySet())\n {\n...
3
0
['Java']
1
k-diff-pairs-in-an-array
Python 3 (60ms) | O(n) Counter Hashmap Solution | Easy to Understand
python-3-60ms-on-counter-hashmap-solutio-r1gc
\nclass Solution:\n def findPairs(self, nums: List[int], k: int) -> int:\n count = Counter(nums)\n if k > 0:\n return sum([i + k in
MrShobhit
NORMAL
2022-02-09T14:27:39.076516+00:00
2022-02-09T14:27:39.076560+00:00
852
false
```\nclass Solution:\n def findPairs(self, nums: List[int], k: int) -> int:\n count = Counter(nums)\n if k > 0:\n return sum([i + k in count for i in count])\n else:\n return sum([count[i] > 1 for i in count])\n```
3
0
['Python', 'Python3']
1
k-diff-pairs-in-an-array
[🔥🔥][JAVA][SIMPLE][EASY][SET][NO SORT]☑️
javasimpleeasysetno-sort-by-bharathkalya-hu7v
Simplified Clean Coded Solution !\uD83D\uDE00\n\nclass Solution {\n public int findPairs(int[] nums, int k) {\n int pairs = 0, n = nums.length;\n
bharathkalyans
NORMAL
2022-02-09T12:54:45.618086+00:00
2022-02-09T12:55:03.063323+00:00
159
false
Simplified Clean Coded Solution !\uD83D\uDE00\n```\nclass Solution {\n public int findPairs(int[] nums, int k) {\n int pairs = 0, n = nums.length;\n \n HashSet<Integer> set = new HashSet<>();\n HashSet<Integer> repeatedElements = new HashSet<>();\n \n for(int ele : nums){\n...
3
0
['Ordered Set', 'Java']
0
k-diff-pairs-in-an-array
C++ / Python Simple and Clean O(n) Solutions
c-python-simple-and-clean-on-solutions-b-lgt0
C++:\n\nclass Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n unordered_map<int, int> map;\n for(auto num:nums)\n
yehudisk
NORMAL
2022-02-09T08:45:27.341241+00:00
2022-02-09T08:45:27.341274+00:00
267
false
**C++:**\n```\nclass Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n unordered_map<int, int> map;\n for(auto num:nums)\n map[num]++;\n \n int res = 0;\n if (k > 0) {\n for(auto a:map)\n if (map.find(a.first+k) != map.end()) \n ...
3
0
['C', 'Python']
0
k-diff-pairs-in-an-array
C++| Two pointers | Lower Bound | nlogn
c-two-pointers-lower-bound-nlogn-by-vaib-itiw
Using two pointer C++ | nlogn :\n\nint findPairs(vector<int>& nums, int k) {\n int ans=0;\n sort(nums.begin(),nums.end());\n int i=0,j=1;\n
vaibhavshekhawat
NORMAL
2022-02-09T05:39:15.648921+00:00
2022-02-09T05:40:43.321381+00:00
219
false
### Using two pointer C++ | nlogn :\n```\nint findPairs(vector<int>& nums, int k) {\n int ans=0;\n sort(nums.begin(),nums.end());\n int i=0,j=1;\n int n=nums.size();\n while(i<n &&j<n){ \n if(i!=0 && nums[i]==nums[i-1]){i++;continue;}\n ...
3
0
['C']
1
k-diff-pairs-in-an-array
[Java/Binary Search] Easy to understand
javabinary-search-easy-to-understand-by-uz9hu
Try search for each element because we need to find only unique element so discard those element that have been traversed,\n\nclass Solution \n{\n public int
indiankiller
NORMAL
2022-02-09T05:04:53.848652+00:00
2022-02-09T05:04:53.848699+00:00
277
false
Try search for each element because we need to find only unique element so discard those element that have been traversed,\n```\nclass Solution \n{\n public int findPairs(int[] nums, int k) \n {\n int n = nums.length;\n int count =0;\n Arrays.sort(nums);\n if(k==0)\n {\n ...
3
0
['Binary Tree', 'Java']
0
k-diff-pairs-in-an-array
[Python3] Runtime: 58 ms, faster than 99.40% | Memory: 15.6 MB, less than 89.25%
python3-runtime-58-ms-faster-than-9940-m-p4cw
\nclass Solution:\n\tdef findPairs(self, nums: List[int], k: int) -> int:\n\t\tans,check = 0,{}\n\t\tfor i in nums:\n\t\t\tif i not in check:\n\t\t\t\tcheck[i]=
anubhabishere
NORMAL
2022-02-09T03:35:15.578143+00:00
2022-02-09T03:35:15.578181+00:00
110
false
```\nclass Solution:\n\tdef findPairs(self, nums: List[int], k: int) -> int:\n\t\tans,check = 0,{}\n\t\tfor i in nums:\n\t\t\tif i not in check:\n\t\t\t\tcheck[i]=1\n\t\t\telse:\n\t\t\t\tcheck[i]+=1\n\t\tif k>0:\n\t\t\tfor j in check:\n\t\t\t\tif j+k in check.keys():\n\t\t\t\t\tans+=1\n\t\telse:\n\t\t\tfor j in check:\...
3
1
['Python']
0
k-diff-pairs-in-an-array
C++ | Easy Understood Solution | map
c-easy-understood-solution-map-by-parth_-kad6
We need to find pair whose differense is k.\nSo that we can store frequency to map and we will find, whether another key is exist or not whose difference is k.\
parth_chovatiya
NORMAL
2022-02-09T02:59:25.624957+00:00
2022-02-09T02:59:25.624999+00:00
371
false
We need to find pair whose differense is k.\nSo that we can store frequency to map and we will find, whether another key is exist or not whose difference is k.\nif exist, then we will increment our ans by 1.\n\nLet\'s look at one example.\n**nums = [1,3,5,1,4], k = 2**\n\nNow store frequency to map:\n1-> 2\n3-> 1\n4-> ...
3
0
[]
0
k-diff-pairs-in-an-array
C++ Two solutions
c-two-solutions-by-brown_wolf_26-ubdv
\nclass Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n // time O(nlgn) space O(1)\n // sort(nums.begin(),nums.end());\n
scorpion_lone_wolf
NORMAL
2022-01-25T10:31:41.231292+00:00
2022-01-25T10:31:41.231332+00:00
260
false
```\nclass Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n // time O(nlgn) space O(1)\n // sort(nums.begin(),nums.end());\n // int n=nums.size();\n // int count=0;\n // for(int i=0;i<n;i++){\n // if(i!=0&&nums[i]==nums[i-1])continue;\n // bo...
3
0
['Binary Tree']
0
k-diff-pairs-in-an-array
100% faster || 8 ms || without using map || Worst Case O(n logn) + O(n)
100-faster-8-ms-without-using-map-worst-9f4o5
\nclass Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n sort(nums.begin(), nums.end());\n int slow = 0, fast = 1, ans = 0;\n
user0382o
NORMAL
2021-12-12T17:55:00.121881+00:00
2021-12-12T17:55:00.121911+00:00
213
false
```\nclass Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n sort(nums.begin(), nums.end());\n int slow = 0, fast = 1, ans = 0;\n while(slow < nums.size() && fast < nums.size()){\n if(nums[fast] - nums[slow] == k){\n slow++;\n fast++;\n ...
3
0
['C', 'Sorting', 'C++']
0
k-diff-pairs-in-an-array
C++ || Hashmap
c-hashmap-by-yasshpathak-ryxv
\nclass Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n \n int n=nums.size();\n unordered_map<int,int>mymap;\n
yasshpathak
NORMAL
2021-12-10T13:40:21.785426+00:00
2021-12-10T13:40:21.785455+00:00
241
false
```\nclass Solution {\npublic:\n int findPairs(vector<int>& nums, int k) {\n \n int n=nums.size();\n unordered_map<int,int>mymap;\n int count=0;\n \n \n for(int i=0;i<n;i++){\n mymap[nums[i]]++;\n }\n \n if(k==0){\n for(auto ...
3
0
['C']
0