Skip to content

Instantly share code, notes, and snippets.

@arielm
Created September 11, 2016 15:10
Show Gist options
  • Save arielm/d29c7fd200c75f233e47f35292f96149 to your computer and use it in GitHub Desktop.
Save arielm/d29c7fd200c75f233e47f35292f96149 to your computer and use it in GitHub Desktop.
Solution (81%) to Triangle problem on Codility (https://codility.com/programmers/task/triangle)
/*
* CORRECTNESS: 100%
* PERFORMANCE: 50%
*
* FOR A 100% SOLUTION, SEE:
* https://codesays.com/2014/solution-to-triangle-by-codility/
*/
class Solution
{
public int solution(int[] A)
{
int N = A.length;
if (N >= 3)
{
for (int i = 0; i < N - 2; i++)
{
int N2 = (N - 2) - i;
for (int i2 = 0; i2 < N2; i2++)
{
int P = i;
int Q = i + 1;
int R = i + 2 + i2;
if (checkTriplet(A, P, Q, R))
{
return 1;
}
}
int N3 = N2 - 1;
for (int i3 = 0; i3 < N3; i3++)
{
int P = i;
int Q = i + 2;
int R = i + 3 + i3;
if (checkTriplet(A, P, Q, R))
{
return 1;
}
}
}
}
return 0;
}
boolean checkTriplet(int[] A, int P, int Q, int R)
{
if ((long)A[P] + (long)A[Q] > (long)A[R])
{
if ((long)A[Q] + (long)A[R] > (long)A[P])
{
return ((long)A[R] + (long)A[P] > (long)A[Q]);
}
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment