-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecursion.cpp
60 lines (51 loc) · 1.36 KB
/
recursion.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
#include<iostream>
using namespace std;
int fibonacci(int n) {
if (n == 1 || n == 2)
return 1;
return (fibonacci(n-1) + fibonacci(n-2));
}
int sumOfArray(int *arr,int start, int n) {
if (n == 0)
return 0;
if (start == n-1)
return arr[start];
return (arr[start] + sumOfArray(arr, start+1, n));
}
bool numberInArray(int arr[], int n, int x, int start) {
if (start == n)
return false;
if (arr[start] == x)
return true;
return numberInArray(arr, n, x, start+1);
}
int tripletSum(int arr[], int n, int num) {
int count {};
for(int i = 0; i < n; ++i) {
for(int j = i+1; j < n; ++j) {
pair<int, int> p1 = make_pair(arr[i], arr[j]);
int temp = p1.first + p1.second;
for(int k = j+1; k < n; ++k) {
if (num-temp == arr[k]) {
count++;
cout << p1.first << " " << p1.second << " " << arr[k] << endl;
}
}
}
}
return count;
}
int main() {
int n, x;
cin >> n;
int arr[n];
for(int i = 0; i < n; ++i) {
cin >> arr[i];
}
cin >> x;
// cout << fibonacci(n) << endl;
// cout << sumOfArray(arr,0, n) << endl;
// numberInArray(arr, n, x, 0) ? cout << "True\n" : cout << "False\n";
cout << tripletSum(arr, n, x) << endl;
return 0;
}