AlgoMaster Logo

Minimum Size Subarray Sum

Ashish

Ashish Pratap Singh

medium

Problem Description

Solve it on LeetCode

Approaches

1. Brute Force

Intuition:

The simplest way to solve this problem is to consider each possible subarray of the given array. For each subarray, calculate the sum and check if it is greater than or equal to the given target sum s. The length of such subarray should be noted, and at the end, we need the minimum of such lengths.

Code:

2. Sliding Window

Intuition:

A more optimal solution involves using a sliding window technique. The main idea is to maintain a window that contains a sum greater than or equal to s. We expand the window by moving the end pointer and keep shrinking it from the start as long as the desired sum condition is satisfied. This helps in reducing the subarray size while maintaining the sum constraint.

Code: