forked from ishikalohia/algorithm_
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpairswithdifferencek
More file actions
59 lines (47 loc) · 1.1 KB
/
pairswithdifferencek
File metadata and controls
59 lines (47 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<iostream>
using namespace std;
#include "solution.h"
#include<unordered_map>
void printPairs(int *input, int n, int k) {
// Write your code here
unordered_map<int,int> m;
for(int i=0;i<n;i++){
int key=input[i];
m[key]++;
}
for(int i=0;i<n;i++){
int key=input[i];
if(k==0){
int a=m[key];
int count=((a-1)*a)/2;
for(int j=0;j<count;j++){
cout<<key<<" "<<key<<endl;
}
}
else{
int a=key+k;
int b=key-k;
int count=m[key];
int count1=m[a];
int count2=m[b];
for(int j=0; j<count*count1;j++){
cout<<min(a,key)<<" "<<max(a,key)<<endl;
}
for(int j=0; j<count*count2;j++){
cout<<min(b,key)<<" "<<max(b,key)<<endl;
}
}
m.erase(key);
}
}
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;
printPairs(input, n, k);
}