-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathTriangle.java
36 lines (27 loc) · 1.04 KB
/
Triangle.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.Arrays;
class Solution {
public int solution(int[] A) {
// sort array
// linearly check for triangle property
Arrays.sort(A);
for(int i=0; i<A.length - 2;i++) {
if(checkForTriangleProperty(A, i)) { return 1; }
}
return 0;
}
private boolean checkForTriangleProperty(int[] A, int startIndex) {
if( calcSum(A[startIndex], A[startIndex+1]) > A[startIndex+2] &&
calcSum(A[startIndex+2], A[startIndex]) > A[startIndex+1] &&
calcSum(A[startIndex+1], A[startIndex+2]) > A[startIndex] ) { return true; }
return false;
}
// when max int is possible,
// ..sum should handle in long - since it cannot fit and overflow
private long calcSum(int a, int b) {
return (new Long(a) + new Long(b));
}
}