FastPrepMax Consecutive Ones III

Max Consecutive Ones III

Nykaa logoNykaa● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Given a binary array nums and an integer k, return the maximum number of consecutive ones obtainable by flipping at most k zeros to ones.

Function

longestOnes(nums: int[], k: int) → int

Examples

Example 1

nums = [1,1,1,0,0,0,1,1,1,1,0]k = 2return = 6

Flipping two zeros creates a six-element run.

Example 2

nums = [0,0,1,1,1,0,0]k = 0return = 3

Without flips, the central run has length three.

Example 3

nums = [0,0,0]k = 3return = 3

All three zeros can be flipped.

Constraints

  • 0 ≤ nums.length ≤ 100000.
  • Every value in nums is 0 or 1.
  • 0 ≤ k ≤ nums.length.

Source note: Author-written prompt excerpt retained for source fidelity.

More Nykaa problems

See Nykaa hiring insights
public int longestOnes(int[] nums, int k) {
    // write your code here
}
nums[1,1,1,0,0,0,1,1,1,1,0]
k2
expected6
Checking account…