Skip to content

Instantly share code, notes, and snippets.

@tiagopereira17
Created July 6, 2016 15:12
Show Gist options
  • Save tiagopereira17/9dd79a40a5fc3cc61d810a76adfda144 to your computer and use it in GitHub Desktop.
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.
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