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

38. Minimum Size Subarray Sum

MediumDay 12 of 60Sliding Window

Given an array of positive integers nums and a positive integer target, return the length of the shortest contiguous subarray whose sum is at least target. If no subarray reaches target, return 0.

Example 1
Input: target = 7, nums = [2,3,1,2,4,3]
Output: 2
The subarray [4,3] sums to 7 and no single element reaches 7.
Example 2
Input: target = 11, nums = [1,1,1,1,1,1,1,1]
Output: 0
Even the whole array only sums to 8.
Constraints
  • 1 <= target <= 10^9
  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^4
Hints
Frequently asked at: Google, Amazon, Meta, Goldman Sachs
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