forked from Mickey0521/Codility
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Triangle.java
28 lines (21 loc) · 825 Bytes
/
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
package Triangle;
// note: need to import (so, we can use "Arrays.sort(int[])")
import java.util.*;
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
// main idea: for any combination (A[i-2], A[i-1], A[i])
// we just need to check if A[i-2] + A[i-1] > A[i] (important)
// note: A[i-2] + A[i-1] is the max possible combination (needed to check)
// Using "Arrays.sort(int[])"
Arrays.sort(A);
// note: start from i=2
for(int i=2; i< A.length; i++){
if((long)A[i-2] + (long)A[i-1] > (long)A[i]) // note: using "long" for overflow cases
return 1;
// note: we just need one combination
}
// otherwise, return 0 (no triangular)
return 0;
}
}