forked from markhary/codility
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Triangle.cpp
54 lines (46 loc) · 1.62 KB
/
Triangle.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
// https://app.codility.com/programmers/lessons/6-sorting/triangle/
//
// Task Score: 100%
// Correctness: 100%
// Performance: 100%
// Detected time complexity: O(N*log(N))
#include <algorithm>
#include <vector>
using namespace std;
// This tells us the following: (0<=P<Q<R)
// A triplet will be sequential in a sorted list
// - numbers will be right next to each other, there could be overlapping triplets
// - but there must be one set with three right next to each other
// Be careful of int overflow, do math in long long space
int solution(vector<int> &A) {
int N = A.size();
// Can return immediately if N doesn't have at least three elements to
// be a triangle
if ( N < 3 ) {
return 0;
}
// Sort the array using built in sort, this is O(N*log(N))
std::sort(A.begin(), A.end());
// Last triplet set is going to be two less than end
// This is O(N)
for (int i=0; i<(N-2); i++) {
// Check the final conditions:
// * A[P] + A[Q] > A[R],
// * A[Q] + A[R] > A[P],
// * A[R] + A[P] > A[Q].
// Again, the 'i+0' makes this easier to read
// This is a sorted list so A[P] <= A[Q] <= A[R]
// promote these to avoid int overflow
long long A_p = A[i+0];
long long A_q = A[i+1];
long long A_r = A[i+2];
if ( ((A_p+A_q) > A_r ) &&
((A_q+A_r) > A_p ) &&
((A_r+A_p) > A_q ) ) {
// return 1 the instant we find our first triplet
return 1;
}
}
// Nothing was found if we exited the loop, return 0
return 0;
}