
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
K-diff Pairs in an Array in C++
Suppose we have an array and an integer k, we have to find the number of unique k-diff pairs in the array. Here the k-diff pair is like (i, j), where i and j are both are present in the array and their absolute difference is k.
So, if the input is like [3,1,4,1,5], k = 2, then the output will be 2, as there are two 2-diff pairs in the array-like (1,3) and (3,5).
To solve this, we will follow these steps −
Define maps called seen and done
Define one set s
if k < 0, then −
return 0
for initialize i := 0, when i < size of nums, update (increase i by 1), do −
(increase seen[nums[i]] by 1)
insert nums[i] into s
ans := 0
for each element it in s, do −
if k is same as 0, then −
if seen[it] > 1, then −
(increase ans by 1)
Otherwise
increase done[it] by 1
if (it + k) is in seen but not in done, then −
(increase ans by 1)
if (it - k) is in seen but not in done, then −
(increase ans by 1)
return ans
Example
Let us see the following implementation to get a better understanding −
#include <bits/stdc++.h&g; using namespace std; class Solution { public: int findPairs(vector<int>& nums, int k) { map<int, int> seen, done; set<int> s; if (k < 0) return 0; for (int i = 0; i < nums.size(); i++) { seen[nums[i]]++; s.insert(nums[i]); } int ans = 0; for (auto it = s.begin(); it != s.end(); it++) { if (k == 0) { if (seen[*it] > 1) ans++; } else { done[*it]++; if (seen.find(*it + k) != seen.end() && done.find(*it + k) == done.end()) ans++; if (seen.find(*it - k) != seen.end() && done.find(*it - k) == done.end()) ans++; } } return ans; } }; main(){ Solution ob; vector<int> v = {3,1,4,1,5}; cout << (ob.findPairs(v, 2)); }
Input
{3,1,4,1,5}, 2
Output
2
- Related Articles
- K Inverse Pairs Array in C++
- Find the maximum cost of an array of pairs choosing at most K pairs in C++
- Count all pairs of an array which differ in K bits in C++
- Find all pairs (a, b) in an array such that a % b = k in C++
- Count pairs in array whose sum is divisible by K in C++
- Count divisible pairs in an array in C++
- Program to find out the k-th smallest difference between all element pairs in an array in C++
- Check if an array can be divided into pairs whose sum is divisible by k in Python
- Count pairs in a sorted array whose product is less than k in C++
- Program to count nice pairs in an array in Python
- How to convert nested array pairs to objects in an array in JavaScript ?
- Check if a sorted array can be divided in pairs whose sum is k in Python
- Sum of XOR of all pairs in an array in C++
- The k Strongest Values in an Array in C++
- Find K Pairs with Smallest Sums in C++
