import java.util.*;
public class Main {
public static int solution(int[] arr){
int j = 0 , count = 0 , ans = 0;
//acquiring loop
for(int i = 0 ; i < arr.length ; i++){
if(arr[i] == 0)
count++;
//releasing loop till count becomes valid again
while(count > 1){
if(arr[j] == 0)
count--;
j++;
}
//update answer
int len = i - j + 1;
ans = Math.max(len , ans);
}
return ans;
}
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int[] arr = new int[n];
for(int i = 0 ; i < n; i++){
arr[i] = scn.nextInt();
}
System.out.println(solution(arr));
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter