Find the longest run of ones after flipping at most k zeros

Quick Overview

Given a binary array and an integer k, return the length of the longest run of consecutive ones you can form by flipping at most k zeros. It tests linear-time two-pointer reasoning on arrays and edge cases such as k equal to zero or k larger than the number of zeros.

Find the longest run of ones after flipping at most k zeros

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given a binary array `nums` and an integer `k`, return the maximum number of consecutive `1`s in the array if you can flip at most `k` `0`s. A flip changes a single `0` into a `1`. You may use fewer than `k` flips. ### Function Signature ```python def longest_ones(nums: list[int], k: int) -> int: ``` ### Rules - The answer is the length of the longest contiguous run of `1`s after the flips. If no run can be formed (for example `k = 0` and `nums` has no `1`), return 0. ### Constraints - `1 <= len(nums) <= 10^5` - Each `nums[i]` is either `0` or `1`. - `0 <= k <= len(nums)` ### Examples ```text Input: nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2 Output: 6 ``` Flipping the zeros at indices 4 and 5 turns indices 4 to 9 into six consecutive `1`s. Flipping indices 5 and 10 also gives a run of 6 (indices 5 to 10), and no choice of two flips does better. ```text Input: nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], k = 3 Output: 10 ``` Flipping the zeros at indices 4, 5 and 9 turns indices 2 to 11 into ten consecutive `1`s. ```text Input: nums = [0,0,0], k = 0 Output: 0 ``` No flips are allowed and the array has no `1`s, so there is no run.

Overview: Given a binary array and an integer k, return the length of the longest run of consecutive ones you can form by flipping at most k zeros. It tests linear-time two-pointer reasoning on arrays and edge cases such as k equal to zero or k larger than the number of zeros.

PracHub logo
General
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given a binary array nums and an integer k, return the maximum number of consecutive 1s in the array if you can flip at most k 0s.

A flip changes a single 0 into a 1. You may use fewer than k flips.

Function Signature

def longest_ones(nums: list[int], k: int) -> int:

Rules

  • The answer is the length of the longest contiguous run of 1 s after the flips. If no run can be formed (for example k = 0 and nums has no 1 ), return 0.

Constraints

  • 1 <= len(nums) <= 10^5
  • Each nums[i] is either 0 or 1 .
  • 0 <= k <= len(nums)

Examples

Input: nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2
Output: 6

Flipping the zeros at indices 4 and 5 turns indices 4 to 9 into six consecutive 1s. Flipping indices 5 and 10 also gives a run of 6 (indices 5 to 10), and no choice of two flips does better.

Input: nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], k = 3
Output: 10

Flipping the zeros at indices 4, 5 and 9 turns indices 2 to 11 into ten consecutive 1s.

Input: nums = [0,0,0], k = 0
Output: 0

No flips are allowed and the array has no 1s, so there is no run.