Bytes
rocket

Your Success, Our Mission!

6000+ Careers Transformed.

Optimized Sliding Window Approach

Last Updated: 3rd September, 2026

Imagine your study streak :

  • You move day by day forward (right pointer)
  • You allow only K bad days

If bad days exceed K:
You don’t restart from beginning
You just remove earlier days from your streak (move left)

This way:

You always maintain the best valid streak without repeating work

high (1).png

Core Idea (Game Changer)

The brute force method wastes time by restarting again and again.
The sliding window approach fixes this by saying:

“Don’t restart, just adjust the current window.”

Instead of checking every subarray:

  • We maintain a continuous window
  • Expand it when valid
  • Shrink it when invalid

Intuition (Very Simple)

Think like this:

You have a window [left → right]
You are allowed at most K zeros inside this window

  • If zeros ≤ K → ✅ valid → expand (move right)
  • If zeros > K → ❌ invalid → shrink (move left)

Step-by-Step Working

  1. Start with:
left = 0
zero_count = 0
  1. Move right pointer:
If nums[right] == 0 → increase zero count
  1. If zero_count > K:
    • Move left forward
    • Reduce zero count if needed
  2. Update answer:
max_length = max(max_length, rightleft1)

Dry Run Example

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

Window

Zeros

Action

Start[1]0Expand
[1,0]1Expand
[1,0,1,1]1Expand
[1,0,1,1,0]2Expand
[1,0,1,1,0,0]3❌ Shrink
AdjustValid again2Continue

Final Answer = 5

Code

def longestOnes(nums, k):
  left0
  zero_count = 0
  max_length = 0

  for right in range(len(nums)):
      if nums[right] == 0:
          zero_count += 1

      while zero_count > k:
          if nums[left] == 0:
              zero_count -= 1
          left += 1

      max_length = max(max_length, rightleft1)

  return max_length

Complexity

  • Time Complexity: O(n) ✅
  • Space Complexity: O(1) ✅

Each element is visited at most twice

Why This Is Powerful

✔ No repeated work
✔ Real-time adjustment
✔ Works efficiently for large inputs
✔ Most important pattern for exams/interviews

Module 3: Optimized Sliding Window ApproachOptimized Sliding Window Approach

Top Tutorials

Logo

Top 10 Machine Learning Projects with Source Code (Beginner to Advanced)

Explore the Top 10 Machine Learning projects with source code, from beginner to advanced. Learn real-world ML applications, build portfolio-ready projects, and master hands-on skills with step-by-step tutorials from AlmaBetter.

3 Modules10 Lessons1151 Learners
Start Learning
Logo
Careers in Tech

Technologies to Learn in 2026: Building the Future of Innovation

Explore the top technologies to learn in 2026 including Generative AI, Cloud, Cybersecurity, Web3, Data Science, AR/VR, Quantum, RPA, and Green Tech.

00 Lessons909 Learners
Start Learning
Logo
Careers in Tech

aws

This tutorial presents a structured, beginner-focused yet industry-aligned guide to Amazon Web Services, designed specifically for 2026 learning and career requirements

00 Lessons42 Learners
Start Learning
  • Official Address
  • 4th floor, 133/2, Janardhan Towers, Residency Road, Bengaluru, Karnataka, 560025
  • Communication Address
  • Follow Us
  • facebook
    instagram
    linkedin
    twitter
    youtube
    telegram

© 2026 AlmaBetter