Dsa
Two Pointers
Easy
Maximal Consecutive Ones
You are given a binary array consisting of only 0's and 1's. Your task is to find the maximum number of consecutive 1's in the array that can be obtained by flipping at most one 0 to a 1. Write a function that takes the input data and returns the maximum length of consecutive 1's obtainable.
Input Format:
- The first line contains an integer n (1 ≤ n ≤ 1000) — the size of the binary array.
- The second line contains n space-separated integers (0 or 1) — the binary elements of the array.
Output Format:
- Print the maximum length of consecutive 1's obtainable by flipping at most one 0.
Example:
Input:
10
1 1 0 1 1 1 0 1 1 1
Output:
7
Key concepts
two_pointersarraybinary
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now