Maximum Perimeter Triangle

  • + 0 comments

    Java

    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;
             }
    
    }