K-diff Pairs in an Array – LeetCode Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.
Given an array of integers nums
and an integer k
, return the number of unique k-diff pairs in the array.
A k-diff pair is an integer pair (nums[i], nums[j])
, where the following are true:
0 <= i, j < nums.length
i != j
nums[i] - nums[j] == k
Notice that |val|
denotes the absolute value of val
.
Example 1:
Input: nums = [3,1,4,1,5], k = 2 Output: 2 Explanation: 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 unique pairs.
Example 2:
Input: nums = [1,2,3,4,5], k = 1 Output: 4 Explanation: There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
Example 3:
Input: nums = [1,3,1,5,4], k = 0 Output: 1 Explanation: There is one 0-diff pair in the array, (1, 1).
Constraints:
1 <= nums.length <= 104
-107 <= nums[i] <= 107
0 <= k <= 107
C++ K-diff Pairs in an Array LeetCode Solution
class Solution {
public:
int findPairs(vector<int>& nums, int k) {
sort(nums.begin(),nums.end());
int ans=0,i=0,j=1;
for(i,j;i<nums.size() and j<nums.size();)
{
if(i==j or nums[j]-nums[i]<k)
j++;
else
{
if(nums[j]-nums[i]==k)
{
ans++;
j++;
for(;j<nums.size();j++)
if(nums[j]!=nums[j-1])
break;
if(j==nums.size())
return ans;
j--;
}
i++;
while(i<j and nums[i]==nums[i-1])
i++;
}
}
return ans;
}
};
Java K-diff Pairs in an Array LeetCode Solution
public class Solution {
public int findPairs(int[] nums, int k) {
if (nums == null || nums.length == 0 || k < 0) return 0;
Map<Integer, Integer> map = new HashMap<>();
int count = 0;
for (int i : nums) {
map.put(i, map.getOrDefault(i, 0) + 1);
}
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (k == 0) {
//count how many elements in the array that appear more than twice.
if (entry.getValue() >= 2) {
count++;
}
} else {
if (map.containsKey(entry.getKey() + k)) {
count++;
}
}
}
return count;
}
}
Python 3 K-diff Pairs in an Array LeetCode Solution
def findPairs(self, nums, k):
res = 0
c = collections.Counter(nums)
for i in c:
if k > 0 and i + k in c or k == 0 and c[i] > 1:
res += 1
return res
which equals to:
def findPairs(self, nums, k):
c = collections.Counter(nums)
return sum(k > 0 and i + k in c or k == 0 and c[i] > 1 for i in c)
Array-1180
String-562
Hash Table-412
Dynamic Programming-390
Math-368
Sorting-264
Greedy-257
Depth-First Search-256
Database-215
Breadth-First Search-200
Tree-195
Binary Search-191
Matrix-176
Binary Tree-160
Two Pointers-151
Bit Manipulation-140
Stack-133
Heap (Priority Queue)-117
Design-116
Graph-108
Simulation-103
Prefix Sum-96
Backtracking-92
Counting-86
Sliding Window-73
Linked List-69
Union Find-66
Ordered Set-48
Monotonic Stack-47
Recursion-43
Trie-41
Binary Search Tree-40
Divide and Conquer-40
Enumeration-39
Bitmask-37
Queue-33
Memoization-32
Topological Sort-31
Geometry-30
Segment Tree-27
Game Theory-24
Hash Function-24
Binary Indexed Tree-21
Interactive-18
Data Stream-17
String Matching-17
Rolling Hash-17
Shortest Path-16
Number Theory-16
Combinatorics-15
Randomized-12
Monotonic Queue-9
Iterator-9
Merge Sort-9
Concurrency-9
Doubly-Linked List-8
Brainteaser-8
Probability and Statistics-7
Quickselect-7
Bucket Sort-6
Suffix Array-6
Minimum Spanning Tree-5
Counting Sort-5
Shell-4
Line Sweep-4
Reservoir Sampling-4
Eulerian Circuit-3
Radix Sort-3
Strongly Connected Componen-t2
Rejection Sampling-2
Biconnected Component-1