Q-16 PepCoding | Maximum Consecutive Ones - 2
Wed Jan 25 2023 15:45:09 GMT+0000 (Coordinated Universal Time)
Saved by
@Ayush_dabas07
import java.util.*;
public class Main {
public static int solution(int[] arr, int k){
int j = 0 , count = 0 , ans = 0;
//acquiring loop
for(int i = 0 ; i < arr.length ; i++){
if(arr[i] == 0)
count++;
//releasing loop
while(count > k){
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();
}
int k = scn.nextInt();
System.out.println(solution(arr,k));
}
}
content_copyCOPY
https://www.pepcoding.com/resources/data-structures-and-algorithms-in-java-levelup/hashmap-and-heaps/maximum-consecutive-ones-ii-official/ojquestion
Comments