Bytes
rocket

Your Success, Our Mission!

6000+ Careers Transformed.

Brute Force Approach

Last Updated: 3rd September, 2026

Imagine you are reading a book  and tracking your focus:

1 → Fully focused

0 → Distracted

You are allowed only K distractions

Now what you do (brute force):

You start from every page

From each page, you keep reading forward
Count how many times you get distracted
If distractions exceed K → you stop and restart from the next page

???? So basically:

You are rechecking the same pages again and again from scratch

lolo (1).png

How It Works

For every index i:

  1. Start a subarray from i
  2. Move forward with index j
  3. Count number of zeros in [i → j]
  4. If zeros ≤ K → update answer
  5. If zeros > K → stop and move to next i

Dry Run Example

nums = [10101]
k = 1

Check all subarrays:

  • Start at index 0:
    • [1] → valid
    • [1,0] → valid (1 zero)
    • [1,0,1] → valid
    • [1,0,1,0] → ❌ invalid (2 zeros > K)
  • Start at index 1:
    • [0] → valid
    • [0,1] → valid
    • [0,1,0] → ❌ invalid

...and so on.

Maximum length found = 3

Code (Brute Force)

def longestOnes(nums, k):
  n = len(nums)
  max_length = 0

  forin range(n):
      zero_count = 0

      forin range(i, n):
          if nums[j] == 0:
              zero_count += 1

          if zero_count > k:
              break

          max_length = max(max_length, j - i + 1)

  return max_length

Time & Space Complexity

  • Time Complexity: O(n²)
    → Two nested loops
  • Space Complexity: O(1)
    → No extra space used

Why Brute Force Fails

Here’s the problem:

It checks too many unnecessary subarrays
Recalculates zero count again and again
Becomes very slow for large inputs (like 10⁵ elements)

Intuition Gap

Notice this:

When we move from one subarray to the next, we are starting from scratch again, even though most elements are the same.

This is the inefficiency.

Key Insight Leading to Optimization

“Why not reuse previous work instead of recalculating everything?”

Instead of restarting every time:

  • We can expand and shrink a window dynamically
  • Keep track of zeros in real-time

This idea leads to the Sliding Window Approach (Optimal Solution)

Module 2: Brute Force ApproachBrute Force 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