http://www.careercup.com/question?id=5689376707182592
public boolean triangleTripletExists(int[] arr) {
mergeSort(arr);
int i, j, k;
i = 0;
j = 1;
k = 2;
for (k = 2; k < arr.length; k++) {
if (isTriplet(arr[i++], arr[j++], arr[k])) {
return true;
}
}
return false;
}
public boolean isTriplet(int a, int b, int c) {
boolean first, second, third;
first = ((a + b) > c);
second = ((a + c) > b);
third = ((c + b) > a);
return (first && second && third);
}
public static ArrayList<Vector> getTriangleSides(int[]segments)
{
int[]elements=segments;
ArrayList<Vector> triplet=new ArrayList<Vector>();
if(elements.length<;3)
return triplet;
else
{
quickSort(0, elements.length-1, elements);
for(int i=0;i<elements.length-2;i++)
{
if(elements[i]+elements[i+1]>elements[i+2])
{
Vector vector=new Vector(3);
vector.add(elements[i]);
vector.add(elements[i+1]);
vector.add(elements[i+2]);
triplet.add(vector);
}
}
return triplet;
}
}
No comments:
Post a Comment