-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1117 from Spider-Testing/master
Added a folder of C++ problems and attached a question of hashmap with optimised solution
- Loading branch information
Showing
1 changed file
with
79 additions
and
0 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
Program's_Contributed_By_Contributors/Cpp_code/Pair_with_difference_k.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
Question:- | ||
|
||
Pairs with difference K | ||
|
||
You are given with an array of integers and an integer K. You have to find and print the count of all such pairs which have difference K. | ||
|
||
Note: Take absolute difference between the elements of the array. | ||
|
||
Input Format: | ||
The first line of input contains an integer, that denotes the value of the size of the array. Let us denote it with the symbol n. | ||
The following line contains n space separated integers, that denote the value of the elements of the array. | ||
The following line contains an integer, that denotes the value of K. | ||
|
||
Output format : | ||
The first and only line of output contains count of all such pairs which have an absolute difference of K. | ||
|
||
Constraints : | ||
0 <= n <= 10^4 | ||
|
||
Time Limit: 1 sec | ||
|
||
Sample Input 1 : | ||
4 | ||
5 1 2 4 | ||
3 | ||
Sample Output 1 : | ||
2 | ||
|
||
Sample Input 2 : | ||
4 | ||
4 4 4 4 | ||
0 | ||
Sample Output 2 : | ||
6 | ||
|
||
Code:- | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
|
||
int getPairsWithDifferenceK(int *arr, int n, int k) | ||
{ | ||
unordered_map<int,int> m; | ||
int ans=0,val=0; | ||
for(int i=0;i<n;i++) | ||
{ | ||
val=arr[i]+k; | ||
ans=ans+m[val]; | ||
|
||
if(k!=0) | ||
{ | ||
val=arr[i]-k; | ||
ans=ans+m[val]; | ||
} | ||
m[arr[i]]++; | ||
} | ||
return ans; | ||
} | ||
|
||
int main() | ||
{ | ||
int n; | ||
cin >> n; | ||
|
||
int *input = new int[n]; | ||
|
||
for (int i = 0; i < n; i++) | ||
{ | ||
cin >> input[i]; | ||
} | ||
|
||
int k; | ||
cin >> k; | ||
|
||
cout << getPairsWithDifferenceK(input, n, k); | ||
|
||
delete[] input; | ||
} |