Codility - Triangle
by ne on 2022-02-16 under Algo/DS/Problems tagged with codility
/**
*An array A consisting of N integers is given. 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].
*
* For example, consider array A such that:
* A[0] = 10 A[1] = 2 A[2] = 5
* A[3] = 1 A[4] = 8 A[5] = 20
*
* Triplet (0, 2, 4) is triangular.
*
* Write a function:
*
* class Solution { public int solution(int[] A); }
*
* that, given an array A consisting of N integers, returns 1 if there exists a triangular triplet for this array and returns 0 otherwise.
*
* For example, given array A such that:
* A[0] = 10 A[1] = 2 A[2] = 5
* A[3] = 1 A[4] = 8 A[5] = 20
*
* the function should return 1, as explained above.
*/
import java.util.Arrays;
public class Triangle {
boolean isTriangular(long p, long q, long r) {
if (p + q > r && p + r > q && q + r > p) return true;
return false;
}
public int solution(int[] A) {
Arrays.sort(A);
for (int i = 0; i < A.length - 2; i++) {
if (isTriangular(A[i], A[i + 1], A[i + 2])) {
return 1;
}
}
return 0;
}
}