-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFind-the-Triplets
60 lines (52 loc) · 1.24 KB
/
Find-the-Triplets
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
/*Given an array of distinct integers.
The task is to find all the triplets such that sum of two elements equals the third element.
Input:
The first line of input contains an integer T denoting the number of test cases.
Then T test cases follow. Each test case consists of two lines.
First line of each test case contains an Integer N denoting size of array and the second line contains N space separated elements.*/
//my new improved solution:
#include<iostream>
using namespace std;
void InsertionSort(int* niz, int velicina) {
for (int i = 1; i < velicina; i++) {
int j = i;
while (j > 0 && niz[j - 1] > niz[j]) {
auto pom = niz[j - 1];
niz[j - 1] = niz[j];
niz[j] = pom;
j--;
}
}
}
void findTriplet(int arr[], int n){
InsertionSort(arr, n);
for (int i = n - 1; i >= 0; i--) {
int j = 0;
int k = i - 1;
while (j < k){
if (arr[i] == arr[j] + arr[k]){
cout << arr[j] << " + " << arr[k] << " = " << arr[i] << endl;
j++;
k--;
}
else if (arr[i] > arr[j] + arr[k])
j++;
else
k--;
}
}
}
void main(){
int t;
cin >> t;
while (t--){
int n;
cin >> n;
int *arr = new int[n];
for (int i = 0; i < n; i++)
cin >> arr[i];
findTriplet(arr, n);
delete[] arr;
}
system("pause>0");
}