Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Minimum Size Subarray Sum problem solution

YASH PAL, 31 July 202419 January 2026

In this Leetcode Minimum Size Subarray Sum problem solution, we have given an array of positive integers nums and a positive integer target, return the minimal length of a contiguous subarray [numsl, numsl+1, …, numsr-1, numsr] of which the sum is greater than or equal to target. If there is no such subarray, return 0 instead.

Leetcode Minimum Size Subarray Sum problem solution

Leetcode Minimum Size Subarray Sum problem solution in Python.

class Solution(object):
    def minSubArrayLen(self, s, nums):
        res = float("inf")
        if(nums):
            i=0
            window,currWindowSum = 0,0
            while(i+window<len(nums)):
                currWindowSum+=nums[i+window]
                if(currWindowSum>=s):
                    while(currWindowSum>=s): 
                        res=min(window+1,res)
                        currWindowSum-=nums[i] 
                        i+=1
                        window-=1
                window+=1
        return 0 if(res==float("inf")) else res

Minimum Size Subarray Sum problem solution in Java.

class Solution {
    public int minSubArrayLen(int s, int[] nums) {
        if(nums.length == 0) return 0;
        int left = 0, right = 0, min = Integer.MAX_VALUE, sum = nums[0];
        while(right < nums.length){
            if(sum < s){
                right++;
                if(right == nums.length) break;
                sum += nums[right];
            }else{
                min = Math.min(min, right-left+1);
                left++;
                sum -= nums[left-1];
            }
        }
        return min == Integer.MAX_VALUE? 0 : min;
    }
}

Problem solution in C++.

class Solution {
public:
    int minSubArrayLen(int s, vector<int>& nums) {
        int left = 0, right = 0;
        int res = INT_MAX;
        int cnt = 0;
        while(right < nums.size()){
            cnt += nums[right];
            while(left <= right && cnt >= s){
                res = min(res, right-left+1);
                cnt -= nums[left];
                left++;
            }
            right++;
        }
        return res == INT_MAX ? 0 : res;
    }
};

Problem solution in C.

int minSubArrayLen(int s, int* nums, int numsSize) {
    int *slowRunner, *fastRunner;
    int sumSoFar;
    int shortest;
    int curLength;
    
    slowRunner = fastRunner = nums;
    sumSoFar = 0;
    shortest = numsSize;
    
    while (fastRunner < nums + numsSize) {
        sumSoFar += *fastRunner;
        fastRunner++;   
   
        while (sumSoFar >= s) {
            sumSoFar -= *(slowRunner++);
            curLength = fastRunner - slowRunner + 1;
            shortest = curLength < shortest ? curLength : shortest; 
        }
    }
    return fastRunner - slowRunner == numsSize ? 0 : shortest;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes