Problem · Array

Max Consecutive Ones III

Learn this problem
MediumInMobi logoInMobiFULLTIMEPHONE SCREEN

Problem statement

Given a binary array nums and an integer k, you may change at most k zeroes into ones.

Return the maximum number of consecutive ones obtainable after those changes.

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

Changing the two zeroes between the first and last groups gives a longest run of six ones.

Example 2

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

No changes are allowed, so the existing run of three ones is optimal.

Constraints

  • 1 <= nums.length <= 10^5
  • nums[i] is 0 or 1.
  • 0 <= k <= nums.length

More InMobi problems

drafts saved locally
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