File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change 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+
You can’t perform that action at this time.
0 commit comments