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.
importjava.util.Scanner;importjava.util.Arrays;importjava.util.Collections;publicclassSolution{publicstaticvoidmain(String[]args){Scannerscan=newScanner(System.in);intN=scan.nextInt();Integer[]stick=newInteger[N];for(inti=0;i<N;i++){stick[i]=scan.nextInt();}scan.close();findTriangle(stick);}/* Greedy Approach: Try to use the biggest sticks first */privatestaticvoidfindTriangle(Integer[]stick){Arrays.sort(stick,Collections.reverseOrder());for(inti=0;i<stick.length-2;i++){if(stick[i]<stick[i+1]+stick[i+2]){System.out.println(stick[i+2]+" "+stick[i+1]+" "+stick[i]);return;}}System.out.println(-1);}}
Let me know if you have any questions.
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 solution - passes 100% of test cases
From my HackerRank solutions.
Hint: Sort the sticks
Time complexity: O(n log n)
Let me know if you have any questions.