Longest Circular Run of Ones with One Flip
Arrays & HashingAuto-graded · 25 test casesLast updated
Tests sliding window technique on a circular binary array to find the maximum consecutive ones achievable by flipping at most one zero.
Problem
You have a binary array bits that wraps around — think of it as a ring where the element after the last position is the element at position 0.
You may change at most one 0 in the array to a 1. Return the longest unbroken sequence of 1s you can form anywhere along this ring.
Because the ring is circular, a valid sequence may start near the end of the array and continue from the beginning. However, each position may appear at most once in a sequence, so the answer can never be greater than the length of the array.
Constraints
1 <= len(bits) <= 10^5bits[i]is either0or1
Examples
Example 1
Input: longest_circular_ones([1, 0, 1, 1, 0, 1])
Output: 5
Example 2
Input: longest_circular_ones([0, 0, 0])
Output: 1
Example 3
Input: longest_circular_ones([1, 1, 1])
Output: 3
Hints
Hint 1
A contiguous segment of any circular array always appears as a plain contiguous window somewhere inside the doubled array `bits + bits`. You don't need to build that copy — virtual indexing with `i % n` works fine.
Hint 2
The problem reduces to: find the longest window in that doubled array that contains at most one `0`, subject to the window never being longer than `n`. Two pointers handle both conditions at once.
Hint 3
The length cap `right - left + 1 <= n` is critical. Without it, an all-ones array or a single-zero array would let the window grow past `n` and count the same index twice. Watch for this off-by-one in your shrink condition.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Reducing circular to linear
Any contiguous segment of length at most n in a circular array of length n maps directly to a plain contiguous window somewhere inside the doubled array bits + bits (length 2n). Instead of allocating that doubled array, we index it virtually: position i in the doubled array corresponds to bits[i % n].
Sliding window with two constraints
After flipping one 0, a valid run is any window that contains at most one 0 and has length at most n. The length cap prevents counting the same physical index twice when the window wraps around.
The two-pointer loop maintains both constraints:
- Advance
rightand count any0encountered. - While the window holds more than one
0or its length exceedsn, shrink fromleft, decounting any0that leaves. - Record the window length after each step.
The all-ones early return is a minor optimisation — the loop would correctly reach n due to the length cap regardless, but the explicit check makes the intent clear.
Why it is correct
- Coverage: every optimal circular segment starting at index
sappears as the virtual window[s, s + L - 1]within[0, 2n), so the global optimum is always reachable. - Monotonicity: if a window is valid (≤ 1 zero, length ≤ n), every sub-window is too. This guarantees
leftnever needs to move backward, making the two-pointer approach sound. - Flip semantics: a window with zero
0s is already all1s; a window with exactly one0becomes all1s after one flip. Windows with two or more zeros are never recorded.
Complexity
- Time: O(n) —
rightadvances2nsteps;leftadvances at most2nsteps in total. - Space: O(1) extra — modular indexing avoids building
bits + bits.
Common pitfall
Omitting the right - left + 1 > n shrink condition. On an input like [1, 1, 0, 1, 1], the window would keep growing through the second copy and return a value greater than n.
Reference solution
from typing import List
def longest_circular_ones(bits: List[int]) -> int:
n = len(bits)
# If the array is already all 1s, the entire ring is one run.
if all(bits):
return n
best = left = zeros = 0
# Slide over the virtual doubled array bits + bits via modular indexing.
for right in range(2 * n):
if bits[right % n] == 0:
zeros += 1
# Shrink the window when it holds more than one 0
# or when it would exceed n (reusing a physical index).
while zeros > 1 or right - left + 1 > n:
if bits[left % n] == 0:
zeros -= 1
left += 1
best = max(best, right - left + 1)
return best