TypeScript Triangle
function triangle(a: number[]): number {
const sorted = [...a].sort((x, y) => x - y);
const c = sorted.length;
if (c < 3) {
return 0;
}
for (let i = 0; i < c - 2; i++) {
if (sorted[i] > 0 && sorted[i] > sorted[i + 2] - sorted[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.