Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

39. Max Consecutive Ones III

MediumDay 12 of 60Sliding Window

Given a binary array nums (only 0s and 1s) and an integer k, you may flip at most k zeros into ones. Return the length of the longest run of consecutive 1s you can get.

Example 1
Input: nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2
Output: 6
Flip the zeros at index 5 and 10; then indices 5 to 10 are all 1s.
Example 2
Input: nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], k = 3
Output: 10
Flipping the zeros at indices 4, 5 and 9 gives ten 1s from index 2 to 11.
Constraints
  • 1 <= nums.length <= 10^5
  • `nums[i]` is 0 or 1
  • 0 <= k <= nums.length
Hints
Frequently asked at: Meta, Amazon, Microsoft, PhonePe
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min