Skip to content

Latest commit

 

History

History
22 lines (17 loc) · 776 Bytes

Triangle.md

File metadata and controls

22 lines (17 loc) · 776 Bytes

Determine whether a triangle can be built from a given set of edges.

Solution (JavaScript)

The insight required here is that for a triange to occur, the numbers must be relatively close in value. If we sort the array, then we can get the numbers in sets of 3 with their closest neightbour, if any are a triangle then we have a solution.

Test Score: 100%

function solution(A) {
    A.sort((a,b) => a-b)
    
    for (let i = 2; i < A.length; ++i) {
        if (A[i] + A[i-1] <= A[i-2] ) continue
        if (A[i] + A[i-2] <= A[i-1] ) continue
        if (A[i-1] + A[i-2] <= A[i] ) continue
        return 1
    }
    
    return 0
}