-
Notifications
You must be signed in to change notification settings - Fork 0
/
Election_604.java
33 lines (30 loc) · 1.03 KB
/
Election_604.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Election_604 {
public static void main(String... args) {
var scanner = new Scanner(System.in);
int n = scanner.nextInt();
System.out.println(winner(n));
}
private static int winner(int total) {
List<Integer> list = new ArrayList<>(total);
for (int i = 0; i < total; i++) {
list.add(i + 1);
}
return winner(1, list);
}
private static int winner(int start, List<Integer> list) {
if (list.size() == 1) return list.get(0);
List<Integer> temp = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
if ((i + 1) % 2 == start)
temp.add(list.get(i));
}
if (start == 1 && list.size() % 2 == 0) return winner(1, temp);
if (start == 1) return winner(0, temp);
if (start == 0 && list.size() % 2 == 0) return winner(0, temp);
if (start == 0) return winner(1, temp);
return -1;
}
}