Skip to content

Commit 14d6071

Browse files
committed
codeChef solution to cut the sticks
1 parent 7c106ce commit 14d6071

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
### https://www.codechef.com/problems/CUTSTICK

0 commit comments

Comments
 (0)