Maximum Perimeter Triangle

  • + 0 comments

    Java

    public static List<Integer> maximumPerimeterTriangle(List<Integer> sticks) {
        // Write your code here
            List<Integer> ans = new ArrayList<>();
            int x = -1;
            Collections.sort(sticks);
            for(int i =0; i<sticks.size()-2; i++)
                if(sticks.get(i)+sticks.get(i+1) > sticks.get(i+2))
                    x = i;
            if(x== -1){
                ans.add(-1);
                return ans;
            } else {
                ans.add(sticks.get(x));
                ans.add(sticks.get(x+1));
                ans.add(sticks.get(x+2));
                return ans;
            }
        }