Tasks Details
easy
1.
Triangle
Determine whether a triangle can be built from a given set of edges.
Task Score
100%
Correctness
100%
Performance
100%
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] = 20Triplet (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] = 20the function should return 1, as explained above. Given array A such that:
A[0] = 10 A[1] = 50 A[2] = 5 A[3] = 1the function should return 0.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [0..100,000];
- each element of array A is an integer within the range [−2,147,483,648..2,147,483,647].
Copyright 2009–2025 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used Java 8
Time spent on task 1 minutes
Notes
not defined yet
Code: 08:33:00 UTC,
java,
autosave
Code: 08:33:03 UTC,
java,
verify,
result: Failed
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
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
// you can also use imports, for example:
// import java.math.*;
import java.util.Arrays;
class Solution {
/**
* Check whether there is a triangular
* @param A The array for length of lines
* @return 0: no triangular found
* 1: triangular is found
*/
public int solution(int[] A) {
// Handle with the special cases
if(null == A || A.length < 3) return 0;
// Sort the input, and then try to find the triangular
Arrays.sort(A);
for(int i = 0; i < A.length-2; i++) {
// Beware of overflow
if (A[i] >= 0 && A[i] > A[i+2] - A[i+1]) {
return 1;
}
/*
* We already know A[i+1] <= A[i+2]. If A[i] < 0,
* A[i] + A[i+1] < A[i+2]
*/
}
return 0;
}
}
Analysis
Compile error
Solution.java:1: error: class, interface, or enum expected 1 ^ Solution.java:2: error: class, interface, or enum expected 2 ^ Solution.java:3: error: class, interface, or enum expected 3 ^ Solution.java:4: error: class, interface, or enum expected 4 ^ Solution.java:5: error: class, interface, or enum expected 5 ^ Solution.java:6: error: class, interface, or enum expected 6 ^ Solution.java:7: error: class, interface, or enum expected 7 ^ Solution.java:8: error: class, interface, or enum expected 8 ^ Solution.java:9: error: class, interface, or enum expected 9 ^ Solution.java:10: error: class, interface, or enum expected 10 ^ Solution.java:11: error: class, interface, or enum expected 11 ^ Solution.java:12: error: class, interface, or enum expected 12 ^ Solution.java:13: error: class, interface, or enum expected 13 ^ Solution.java:14: error: class, interface, or enum expected 14 ^ Solution.java:15: error: class, interface, or enum expected 15 ^ Solution.java:16: error: class, interface, or enum expected 16 ^ Solution.java:17: error: class, interface, or enum expected 17 ^ Solution.java:18: error: class, interface, or enum expected 18 ^ Solution.java:19: error: class, interface, or enum expected 19 ^ Solution.java:20: error: class, interface, or enum expected 20 ^ Solution.java:21: error: class, interface, or enum expected 21 ^ Solution.java:22: error: class, interface, or enum expected 22 ^ Solution.java:23: error: class, interface, or enum expected 23 ^ Solution.java:24: error: class, interface, or enum expected 24 ^ Solution.java:25: error: class, interface, or enum expected 25 ^ Solution.java:26: error: class, interface, or enum expected 26 ^ Solution.java:27: error: class, interface, or enum expected 27 ^ Solution.java:28: error: class, interface, or enum expected 28 ^ Solution.java:29: error: class, interface, or enum expected 29 ^ Solution.java:30: error: class, interface, or enum expected 30 ^ Solution.java:31: error: class, interface, or enum expected 31 ^ 31 errors
Code: 08:33:26 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.math.*;
import java.util.Arrays;
class Solution {
/**
* Check whether there is a triangular
* @param A The array for length of lines
* @return 0: no triangular found
* 1: triangular is found
*/
public int solution(int[] A) {
// Handle with the special cases
if(null == A || A.length < 3) return 0;
// Sort the input, and then try to find the triangular
Arrays.sort(A);
for(int i = 0; i < A.length-2; i++) {
// Beware of overflow
if (A[i] >= 0 && A[i] > A[i+2] - A[i+1]) {
return 1;
}
/*
* We already know A[i+1] <= A[i+2]. If A[i] < 0,
* A[i] + A[i+1] < A[i+2]
*/
}
return 0;
}
}
Analysis
Code: 08:33:30 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
// import java.math.*;
import java.util.Arrays;
class Solution {
/**
* Check whether there is a triangular
* @param A The array for length of lines
* @return 0: no triangular found
* 1: triangular is found
*/
public int solution(int[] A) {
// Handle with the special cases
if(null == A || A.length < 3) return 0;
// Sort the input, and then try to find the triangular
Arrays.sort(A);
for(int i = 0; i < A.length-2; i++) {
// Beware of overflow
if (A[i] >= 0 && A[i] > A[i+2] - A[i+1]) {
return 1;
}
/*
* We already know A[i+1] <= A[i+2]. If A[i] < 0,
* A[i] + A[i+1] < A[i+2]
*/
}
return 0;
}
}
Analysis
Code: 08:33:35 UTC,
java,
final,
score: 
100
// you can also use imports, for example:
// import java.math.*;
import java.util.Arrays;
class Solution {
/**
* Check whether there is a triangular
* @param A The array for length of lines
* @return 0: no triangular found
* 1: triangular is found
*/
public int solution(int[] A) {
// Handle with the special cases
if(null == A || A.length < 3) return 0;
// Sort the input, and then try to find the triangular
Arrays.sort(A);
for(int i = 0; i < A.length-2; i++) {
// Beware of overflow
if (A[i] >= 0 && A[i] > A[i+2] - A[i+1]) {
return 1;
}
/*
* We already know A[i+1] <= A[i+2]. If A[i] < 0,
* A[i] + A[i+1] < A[i+2]
*/
}
return 0;
}
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N*log(N))
expand all
Correctness tests
1.
0.004 s
OK
2.
0.004 s
OK
3.
0.004 s
OK
4.
0.004 s
OK
5.
0.008 s
OK
6.
0.004 s
OK
1.
0.008 s
OK
2.
0.004 s
OK
3.
0.004 s
OK
4.
0.004 s
OK
5.
0.008 s
OK
6.
0.004 s
OK
1.
0.004 s
OK
2.
0.004 s
OK
3.
0.004 s
OK
4.
0.004 s
OK
5.
0.004 s
OK
6.
0.008 s
OK
1.
0.004 s
OK
2.
0.004 s
OK
3.
0.004 s
OK
4.
0.004 s
OK
5.
0.004 s
OK
6.
0.004 s
OK
1.
0.004 s
OK
2.
0.004 s
OK
3.
0.004 s
OK
4.
0.004 s
OK
5.
0.004 s
OK
6.
0.004 s
OK
1.
0.004 s
OK
2.
0.004 s
OK
3.
0.004 s
OK
4.
0.004 s
OK
5.
0.004 s
OK
6.
0.004 s
OK
1.
0.008 s
OK
2.
0.004 s
OK
3.
0.004 s
OK
4.
0.004 s
OK
5.
0.004 s
OK
6.
0.004 s
OK
1.
0.008 s
OK
2.
0.004 s
OK
3.
0.004 s
OK
4.
0.004 s
OK
5.
0.004 s
OK
6.
0.004 s
OK
1.
0.004 s
OK
2.
0.004 s
OK
3.
0.004 s
OK
4.
0.004 s
OK
5.
0.004 s
OK
6.
0.004 s
OK
1.
0.004 s
OK
2.
0.004 s
OK
3.
0.004 s
OK
4.
0.004 s
OK
5.
0.004 s
OK
6.
0.004 s
OK
expand all
Performance tests
1.
0.032 s
OK
2.
0.004 s
OK
3.
0.004 s
OK
4.
0.008 s
OK
5.
0.004 s
OK
6.
0.004 s
OK
1.
0.124 s
OK
2.
0.008 s
OK
3.
0.008 s
OK
4.
0.004 s
OK
5.
0.004 s
OK
6.
0.004 s
OK
1.
0.296 s
OK
2.
0.004 s
OK
3.
0.004 s
OK
4.
0.004 s
OK
5.
0.004 s
OK
6.
0.008 s
OK
1.
0.292 s
OK
2.
0.004 s
OK
3.
0.008 s
OK
4.
0.004 s
OK
5.
0.004 s
OK
6.
0.004 s
OK
1.
0.168 s
OK
2.
0.004 s
OK
3.
0.008 s
OK
4.
0.004 s
OK
5.
0.004 s
OK
6.
0.004 s
OK
1.
0.160 s
OK
2.
0.004 s
OK
3.
0.004 s
OK
4.
0.004 s
OK
5.
0.004 s
OK
6.
0.004 s
OK