-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetPairs.cpp
62 lines (49 loc) · 1.37 KB
/
getPairs.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
# include <iostream>
using namespace std;
void getPairs(int arr[], int size, int key);
int main() {
int size;
cout << "Enter the size of the array :";
cin >> size;
if (size % 2 == 0) {
int arr[size];
cout << endl;
for (int i = 0; i < size; i++) {
cout << "Enter element no. " << (i + 1) << " :";
cin >> arr[i];
}
int key;
cout << "\nEnter the Key :";
cin >> key;
getPairs(arr, size, key);
}
else {
cout << "\nPairs can't be formed." << endl;
}
return 0;
}
void getPairs(int arr[], int size, int key) {
bool flags[size];
int count;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (flags[i] != 1 && flags[j] != 1 && i != j && (arr[i] + arr[j]) % key == 0) {
flags[i] = flags[j] = 1;
count++;
}
}
}
if (count == (size / 2)) {
cout << "\nPairs can be formed." << endl;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (flags[i] != 1 && flags[j] != 1 && i != j && (arr[i] + arr[j]) % key == 0) {
cout << '(' << arr[i] << ',' << arr[j] << ')' << endl;
}
}
}
}
else {
cout << "\nPairs cann't be formed." << endl;
}
}