Problem
You get a binary array nums and an integer k. A binary array has only the values 0 and 1.
You can change at most k items from 0 to 1. After the changes, return the length of the longest subarray that has only 1 items. A subarray is a part of the array with consecutive items.
Example 1
- Input:
- nums = [1,0,1,1,0,0,1,1,1,0], k = 1
- Output:
- 4
- Explanation:
- Change the
0at index 1. Then indexes 0 to 3 have only1items.
Example 2
- Input:
- nums = [1,0,1,1,0,0,1,1,1,0], k = 2
- Output:
- 7
- Explanation:
- Change the items at indexes 4 and 5. Then indexes 2 to 8 have only
1items.
Example 3
- Input:
- nums = [0,0,0], k = 0
- Output:
- 0
- Explanation:
- You cannot change any item, and
numshas no1items.
Constraints
- 1 <= nums.length <= 10^5
- nums[i] is 0 or 1.
- 0 <= k <= nums.length
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
- Maximum Coins From K Consecutive Bags
Coding · Array · Binary Search · Greedy
- Minimum Operations to Reduce X to Zero
Coding · Array · Binary Search · Hash Table
- Minimum Size Subarray Sum
Coding · Array · Binary Search · Prefix Sum
- Shortest Subarray with Sum at Least K
Coding · Array · Binary Search · Heap (Priority Queue)
Sources · 2 references
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 2 records
LinkedIn
Leetcode 1004. Max Consecutive Ones III
Reported Sep 30, 2026 · 12 source reports
JUNIOR, MID_LEVEL, SENIOR, STAFF
Source snapshot: Nov 12, 2026
View sourceMeta
Leetcode 1004. Max Consecutive Ones III
Reported Oct 28, 2025 · 15 source reports
MID_LEVEL, SENIOR, STAFF
Source snapshot: Nov 12, 2026
View source
LeetCode company tags · 4 records
Amazon
Max Consecutive Ones III
Reported date not specified · Source window: 30d
Source snapshot: Oct 20, 2026
Source frequency score: 67.4. This is not an interview probability.
View sourceD. E. Shaw
Max Consecutive Ones III