File tree Expand file tree Collapse file tree 2 files changed +38
-0
lines changed
Beginner/Cut The Sticks (CUTSTICK) Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change 1+ class CutTheSticks {
2+
3+ public static void main (String [] args ) {
4+ List <Integer > sticks = readSticksFromStdin ();
5+ Collections .sort (sticks );
6+
7+ int pos = 0 ;
8+ int remaining = sticks .size ();
9+ while (0 < remaining ) {
10+ System .out .println (remaining );
11+ int count = countEqualFrom (sticks , pos );
12+ pos += count ;
13+ remaining -= count ;
14+ }
15+ }
16+
17+ private static int countEqualFrom (List <Integer > sticks , int from ) {
18+ int value = sticks .get (from );
19+ for (int i = 1 ; from + i < sticks .size (); ++i ) {
20+ if (value != sticks .get (from + i )) {
21+ return i ;
22+ }
23+ }
24+ return sticks .size () - from ;
25+ }
26+
27+ private static List <Integer > readSticksFromStdin () {
28+ Scanner scanner = new Scanner (System .in );
29+ int num = scanner .nextInt ();
30+ List <Integer > sticks = new ArrayList <>(num );
31+ for (int i = 0 ; i < num ; ++i ) {
32+ sticks .add (scanner .nextInt ());
33+ }
34+ return sticks ;
35+ }
36+
37+ }
Original file line number Diff line number Diff line change 1+ ### https://www.codechef.com/problems/CUTSTICK
You can’t perform that action at this time.
0 commit comments