We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
public static List maximumPerimeterTriangle(List sticks) {
// The sum of the lengths of any two sides of a triangle must be greater than the
//length of the third side.
int[] arr=new int[3];
List<Integer> list=new ArrayList<>();
Collections.sort(sticks);
for(int i=sticks.size()-1;i>=2;i--){
arr[0]=sticks.get(i);
arr[1]=sticks.get(i-1);
arr[2]=sticks.get(i-2);
if(arr[0]+arr[1]>arr[2] && arr[1]+arr[2]>arr[0] && arr[2]+arr[0]>arr[1] ){
list.add(arr[0]);
list.add(arr[1]);
list.add(arr[2]);
break;
}else{
continue;
}
}
if(list.size()==0){
list.add(-1);
return list;
}else{
Collections.sort(list);
return list;
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Maximum Perimeter Triangle
You are viewing a single comment's thread. Return to all comments →
Java
public static List maximumPerimeterTriangle(List sticks) {