C++ Triangle
#include <algorithm>
#include <vector>
int triangle(std::vector<int> a)
{
std::sort(a.begin(), a.end());
int c = static_cast<int>(a.size());
if (c < 3) {
return 0;
}
for (int i = 0; i < c - 2; ++i) {
if (a[i] > 0 && a[i] > (a[i + 2] - a[i + 1])) {
return 1;
}
}
return 0;
}
This sorts the values and checks nearby triples, because a valid triangle only needs one local match after sorting.