FastPrepMax Consecutive Ones III
Problems/Max Consecutive Ones III

Max Consecutive Ones III

Amazon logoAmazon● MediumFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an Amazon SDE2 interview list as Max Consecutive Ones III. Problem match confidence: approximately 99%.

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.

More Amazon problems

  • Count Adjacent Symbol Pairs for BPEONSITE INTERVIEW · Seen Nov 2026
  • First Non-Repeating CharacterOA · ONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
See Amazon hiring insights
Saved
class Solution {
    public int longestOnes(int[] nums, int k) {
        // write your code here
    }
}
Checking account…