-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndiaHack1.java
62 lines (52 loc) · 1.2 KB
/
IndiaHack1.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package he;
import java.util.Scanner;
public class IndiaHack1 {
private static Scanner s;
public static void main(String[] args) {
// TODO Auto-generated method stub
s= new Scanner(System.in);
int n=s.nextInt();
int a[]=new int[n];
int l[]=new int[n];
int r[]=new int[n];
int count=0, tcount=0, max=n;
for(int i=0; i<n; i++) {
a[i]=s.nextInt();
if(a[i]==1) {count++; tcount++;}
else count=0;
l[i]=count;
}
count=0;
if(tcount!=n) {
for(int i=n-1; i>=0; i--) {
if(a[i]==1) count++;
else count=0;
r[i]=count;
}
max=0;
for(int i=0; i<n; i++) {
if(a[i]==0) {
if(i==0) {
if(tcount-r[i+1] > 0 && max< r[i+1]+1)
max= r[i+1]+1;
else if(max < r[i+1])
max= r[i+1];
}
else if(i==n-1) {
if(tcount- l[i-1] > 0 && max< l[i-1]+1)
max= l[i-1] +1;
else if(max < l[i-1])
max= l[i-1];
}
else {
if(tcount- (l[i-1] + r[i+1]) > 0 && max< l[i-1] + r[i+1]+1)
max= l[i-1] + r[i+1]+1;
else if(max < l[i-1] + r[i+1])
max= l[i-1] + r[i+1];
}
}
}
}
System.out.println(max);
}
}