-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pairs.cpp
82 lines (72 loc) · 1.38 KB
/
Pairs.cpp
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int counts = 0;
void swaps(int arr[],int target_a,int target_b)
{
int tmp = arr[target_a];
arr[target_a] = arr[target_b];
arr[target_b] = tmp;
}
int partitioning(int _temp_ar[],int s,int e)
{
int pivot = _temp_ar[e];
int j = s - 1;
for(int i = s; i < e; i++)
{
if(_temp_ar[i] <= pivot)
{
counts++;
j = j + 1;
swaps(_temp_ar,i,j);
}
}
swaps(_temp_ar,j+1,e);
return j + 1;
}
void quicksort(int _ar[],int start,int ends)
{
if(start < ends)
{
int q = partitioning(_ar,start,ends);
quicksort(_ar,start,q - 1);
quicksort(_ar,q+1,ends);
}
}
int main()
{
int T,k,counter = 0;
cin>>T>>k;
int ar[T];
for(int i = 0; i < T; i ++)
{
cin>>ar[i];
}
quicksort(ar,0,T - 1);
for(int i = 0; i < T; i++)
{
cout <<ar[i]<<" ";
}
int i = 0; int j = T - 1;
while (i < T) {
while( j > 0)
{
if(ar[j] - ar[i] == k)
{
cout<<ar[j]<<" "<<ar[i]<<endl;
break;
}
j--;
}
if (ar[j] - ar[i] == k)
{
counter++;
// cout<<i<<" "<<j;
}
j = T - 1;
i++;
}
cout << counter;
return 0;
}