Skip to content

Commit 3555ca7

Browse files
authored
Merge pull request AllAlgorithms#199 from khusi-anu/master
update problem
2 parents 2a3dd30 + 5952662 commit 3555ca7

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
3+
You are given with an array of integers and an integer K.
4+
Write a program to find and print all pairs which have difference K.
5+
6+
Sample Input 1 :
7+
4
8+
5 1 2 4
9+
3
10+
Sample Output 1 :
11+
2 5
12+
1 4
13+
*/
14+
15+
16+
#include<iostream>
17+
using namespace std;
18+
19+
20+
void printPairs(int *input, int n, int k) {
21+
22+
int hash[10000];
23+
for(int i = 0;i < n;i++)
24+
{
25+
for(int j = i+1; j < n;j++)
26+
{
27+
if((input[i] - input[j]) == k)
28+
cout << input[j] << " " << input[i] << endl;
29+
else if((input[j] - input[i]) == k)
30+
cout << input[i] << " " << input[j] << endl;
31+
}
32+
}
33+
34+
}
35+
36+
37+
int main() {
38+
int n;
39+
cin >> n;
40+
int *input = new int[n];
41+
for(int i = 0; i < n; i++){
42+
cin >> input[i];
43+
}
44+
int k;
45+
cin >> k;
46+
printPairs(input, n, k);
47+
}
48+

0 commit comments

Comments
 (0)