FastPrepMax Consecutive Ones III

Max Consecutive Ones III

EPAM logoEPAM● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

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

Return the maximum length of a contiguous subarray containing only ones after the 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 two zeroes gives a run of six ones.

Example 2

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

Without changes, the longest existing run has length two.

Constraints

  • 1 <= nums.length <= 100000.
  • Each value is 0 or 1.
  • 0 <= k <= nums.length.

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

More EPAM problems

See EPAM 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…