forked from Mohammed-Shoaib/Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathE0065.cpp
More file actions
33 lines (28 loc) · 663 Bytes
/
E0065.cpp
File metadata and controls
33 lines (28 loc) · 663 Bytes
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
// Problem Code: NOTATRI
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int numberOfTriples(vector<int> L) {
int bound, triples = 0;
sort(L.begin(), L.end());
for (int i = 0 ; i < L.size() - 2; i++)
for (int k, j = i + 1 ; j < L.size() - 1 ; j++) {
bound = L[i] + L[j];
k = j + 1;
triples += L.size() - distance(L.begin(), upper_bound(L.begin() + k, L.end(), bound));
}
return triples;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
while (cin >> n, n > 0) {
vector<int> L(n);
for (int i = 0 ; i < n ; i++)
cin >> L[i];
cout << numberOfTriples(L) << endl;
}
return 0;
}