-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblemD.java
More file actions
54 lines (50 loc) · 1.33 KB
/
ProblemD.java
File metadata and controls
54 lines (50 loc) · 1.33 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.util.*;
public class ProblemD{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int count = 1;
while(sc.hasNextInt()){
int num = sc.nextInt();
if(num == 0){ break; }
int[] jumps = new int[num];
for(int i = 0; i < num; i++){
jumps[i] = sc.nextInt();
}
System.out.println("Circuit #" + count++ + ": " + janice(jumps));
}
}
public static long janice(int[] jumps){
long[] spaces = new long[jumps.length + 1];
spaces[0] = 0;
for(int i = 0; i < jumps.length; i++){
spaces[i+1] = spaces[i] + (long)jumps[i];
}
Arrays.sort(spaces);
long common = spaces[0];
int commonCount = 0;
long contender = spaces[0];
int contenderCount = 0;
for(int i = 0; i < spaces.length; i++){
if(spaces[i] != contender){
if(contenderCount > commonCount || (contenderCount == commonCount && contender < common)){
common = contender;
commonCount = contenderCount;
}
contender = spaces[i];
contenderCount = 0;
}
contenderCount++;
}
if(contenderCount > commonCount || (contenderCount == commonCount && contender < common)){
common = contender;
commonCount = contenderCount;
}
return common;
}
public static void printArray(int[] arr){
for(int i = 0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}
System.out.println();
}
}