Q15 PepCoding | Maximum Consecutive Ones - 1
Wed Jan 25 2023 06:35:52 GMT+0000 (Coordinated Universal Time)
Saved by
@Ayush_dabas07
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));
}
}
content_copyCOPY
https://www.pepcoding.com/resources/data-structures-and-algorithms-in-java-levelup/hashmap-and-heaps/maximum-consecutive-ones-i-official/ojquestion
Comments