Dsa
Sliding Window
Warm-up
Minimum Window Size for Sum
Given an array of positive integers `arr` and a target sum `target`, find the length of the smallest contiguous subarray that has a sum greater than or equal to `target`. If no such subarray exists, return `0`.
Input Format:
- The first line contains space-separated integers representing the array `arr` (1 <= |arr| <= 10000, 1 <= arr[i] <= 10000).
- The second line contains an integer `target` (1 <= target <= 10^9).
Output Format:
- Print an integer representing the minimum length of the subarray with a sum >= target, or `0` if no such subarray exists.
Example:
Input:
2 3 1 2 4 3
7
Output:
2
(The smallest subarray with a sum >= 7 is [4,3].)
Key concepts
sliding_windowarraysconditional_sum
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now