Created
July 6, 2016 15:12
-
-
Save tiagopereira17/9dd79a40a5fc3cc61d810a76adfda144 to your computer and use it in GitHub Desktop.
Determine whether a triangle can be built from a given set of edges.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
public class Triangle { | |
// A triplet (P, Q, R) is triangular if 0 ≤ P < Q < R < N and: | |
// | |
// A[P] + A[Q] > A[R], | |
// A[Q] + A[R] > A[P], | |
// A[R] + A[P] > A[Q]. | |
public int hasTriangle(int[] A) { | |
if(A == null || A.length < 3) { | |
return 0; | |
} | |
Arrays.sort(A); | |
for(int i = 0; i < A.length - 2; i++) { | |
if(A[i] >= 0 && A[i] > A[i+2] - A[i+1]) { | |
return 1; | |
} | |
} | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment