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;publicclassSolution{publicstaticvoidmain(String[]args){/* Save Input */Scannerscan=newScanner(System.in);intnumSticks=scan.nextInt();int[]array=newint[numSticks];for(inti=0;i<numSticks;i++){array[i]=scan.nextInt();}scan.close();Arrays.sort(array);System.out.println(array.length);for(inti=1;i<array.length;i++){if(array[i]!=array[i-1]){System.out.println(array.length-i);}}}}
For input
6544228
we sort the array and get
224458
Our output is
6// we always print the full size of array before the for loop4// since array[2] != array[1]2// since array[4] != array[3]1// since array[5] != array[4]
As we traverse the array from left to right, every time we reach a new number, we can consider that as "cutting the sticks" for the numbers we already traverse.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Cut the sticks
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 array
For input
we sort the array and get
Our output is
As we traverse the array from left to right, every time we reach a new number, we can consider that as "cutting the sticks" for the numbers we already traverse.