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.
staticint[]maximumPerimeterTriangle(int[]s){// Triangle inequality theorem states that// for any non-degenerate triangle, the sum // of the lengths of any two sides must be // greater than the length of the remaining // side.// Sort sticks in ascending order.Arrays.sort(s);// Reverse sticks in decending order.for(intleft=0;left<s.length/2;left++){intright=s.length-1-left;inttmp=s[left];s[left]=s[right];s[right]=tmp;}// Go through the sticks in// descending order.for(inti=0;i<s.length-2;++i){if(s[i]<s[i+1]+s[i+2]){// This is a non-degenerate triangle.// Because we ordered the sticks// in descending order this// triangle must have the maximum// perimeter.returnnewint[]{s[i+2],s[i+1],s[i]};}}returnnewint[]{-1};}
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 implementation with comments: