{"id":2826,"date":"2024-03-24T14:17:46","date_gmt":"2024-03-24T14:17:46","guid":{"rendered":"https:\/\/favtutor.com\/articles\/?p=2826"},"modified":"2025-04-07T12:33:37","modified_gmt":"2025-04-07T12:33:37","slug":"minimum-size-subarray-sum","status":"publish","type":"post","link":"https:\/\/favtutor.com\/articles\/minimum-size-subarray-sum\/","title":{"rendered":"Minimum Size Subarray Sum Problem (C++, Java, Python)"},"content":{"rendered":"\n<p>One of the difficult topics in programming is the <a href=\"https:\/\/favtutor.com\/blogs\/sliding-window-algorithm\">Sliding Window<\/a>. It helps you to solve problems in a lower time complexity. This method is useful when we are solving problems related to subarrays or substrings. In this article, we will discuss the problem \u201cMinimum Size Subarray Sum,\u201d which is based on a sliding window.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u201cMinimum Size Subarray Sum\u201d Problem<\/strong><\/h2>\n\n\n\n<p><strong>In the Minimum Size Subarray problem, you are given an array of integers. You have also been given a target integer. You have to find the minimum length subarray such that the sum of elements present in the subarray is equal to or greater than the target element. <\/strong><\/p>\n\n\n\n<p>Let\u2019s understand this with an example:<\/p>\n\n\n\n<p><strong>Input:<\/strong> <\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background has-link-color wp-elements-ceb297925bb3cbd76106d1d8c165ae8d\" style=\"background-color:#fedcba\"><code>array = &#91; 2, 3, 1, 4, 2, 1], target = 6.<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong> <\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background has-link-color wp-elements-7f377d353b61ef22b359f2258ab5bacd\" style=\"background-color:#fedcba\"><code>2<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong> Shown are different ways in which we can form 6 and the minimum length among them is 2. So the answer is 2.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Brute Force Approach<\/strong><\/h2>\n\n\n\n<p>The basic approach is to create all the subarrays of the given array. Check whose subarray sum is greater than the target element. If it is greater than the target element,t find the minimum length among them. Let\u2019s understand this step by step.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Create a loop that traverses from 1 to n. Where n is the size of the array.<\/li>\n\n\n\n<li>Create a variable \u2018sum\u2019 that stores the total subarray sum.<\/li>\n\n\n\n<li>Create a nested loop that traverses from i to n. Now we get the subarray of size (<strong>j &#8211; i +1<\/strong>).<\/li>\n\n\n\n<li>Check if the subarray sum is greater than the target element or not. If it is, then compare its size with our resultant variable.<\/li>\n\n\n\n<li>If a resultant variable is greater than the size of the subarray. Update the resultant with this size.<\/li>\n\n\n\n<li>After iterating over all the subarrays. Return the resultant variable.<\/li>\n<\/ol>\n\n\n\n<p>The time complexity for the above code is <strong>O(n^2)<\/strong>. Where n is the size of the array. We are using two nested loops whose time complexity is O(n^2). The space complexity for the above code is O(1) as we are not using any extra space.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Binary Search Approach<\/strong><\/h2>\n\n\n\n<p>In the above approach, we see we are using O(n^2) time complexity, which is huge. In this approach, we will calculate the subarray sum for index 1 and store it in a vector. Now we will go at each index and check for their lower bound using <a href=\"https:\/\/favtutor.com\/blogs\/searching-algorithms\">binary search<\/a>. If the size of the lower bound is less than the resultant variable,e, we will update it.\u00a0<\/p>\n\n\n\n<p>Let\u2019s examine this approach step by step.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, create a vector of size array. Next, calculate the subarray sum for index 1.<\/li>\n\n\n\n<li>Now, iterate over each index and find the subarray that has a greater sum than the target element using binary search.<\/li>\n\n\n\n<li>Compare the length of this subarray with the resultant variable. If it is smaller than the resultant variable, then update it.<\/li>\n<\/ul>\n\n\n\n<p>The time complexity for the above code is <strong>O(n log(n))<\/strong>. We first find the subarray sum for index 1, which took O(n) time complexity. Then, we find the subarray sum using a binary search whose time complexity is O( n log(n)). So, the overall time complexity is O( n log(n)).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Optimized Approach: Sliding Window&nbsp;<\/strong><\/h2>\n\n\n\n<p>In the above approaches, we are either using extra time or space. To reduce both of them, we will use the sliding window technique. In this, we will create two variables: start and current. Start to current define a subarray. We will iterate over the array and check if the subarray sum is less than the target or not. If not, we will keep on updating our start index with start +1 and subtracting that element from our sum.<\/p>\n\n\n\n<p>Let\u2019s examine this approach step by step:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>First, create two variables: start and current. Initialize start and current with 0.<\/li>\n\n\n\n<li>Create an answer and a sum variable that stores the minimum length and the total subarray sum. Now iterate over the array and check if the subarray sum is greater than the target or not.<\/li>\n\n\n\n<li>If it is greater than the target, keep on iterating over the loop till <strong>v[current] &#8211; v[start] >= target.<\/strong> Inside that loop, subtract arr[start] from our sum and update <strong>start = start + 1<\/strong>.<\/li>\n\n\n\n<li>Once the iteration is over, check if the <strong>v[current] &#8211; v[start] > = target<\/strong>. If it is update our answer with min(answer, length (subarray)).<\/li>\n\n\n\n<li>At last return the answer.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>C++ Code<\/strong><\/h3>\n\n\n\n<p>Here is the Sliding Window Approach implemented in C++:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-c++src&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;C++&quot;,&quot;modeName&quot;:&quot;cpp&quot;}\">#include &lt;bits\/stdc++.h&gt;\n\nusing namespace std;\n\nint find_min_subarray(vector&lt;int&gt;&amp; arr, int target){\n\n\u00a0\u00a0\u00a0\u00a0int sum=0, answer=INT_MAX, start=0;\n\n\u00a0\u00a0\u00a0\u00a0for(int i=0;i&lt;arr.size();i++){\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0sum+= arr[i];\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0while(sum-arr[start]&gt;=target){\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0sum-= arr[start];\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0start++;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if(sum&gt;=target) answer = min(answer, i - start + 1);\n\n\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0if( answer!=INT_MAX ) return answer;\n\n\u00a0\u00a0\u00a0\u00a0return 0;\n\n}\n\nint main(){\n\n\u00a0\u00a0\u00a0\u00a0vector&lt;int&gt; arr = {2, 3, 1, 4, 2, 1};\n\n\u00a0\u00a0\u00a0\u00a0int target = 6;\n\n\u00a0\u00a0\u00a0\u00a0int answer = find_min_subarray(arr, target);\n\n\u00a0\u00a0\u00a0\u00a0cout&lt;&lt; answer &lt;&lt;endl;\n\n\u00a0\u00a0\u00a0\u00a0return 0;\n\n}<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Java Code<\/strong><\/h3>\n\n\n\n<p>Here is the solution in Java:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-java&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Java&quot;,&quot;modeName&quot;:&quot;java&quot;}\">import java.util.ArrayList;\n\npublic class Main {\n\n\u00a0\u00a0\u00a0\u00a0public static int findMinSubarray(ArrayList&lt;Integer&gt; arr, int target) {\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0int sum = 0, answer = Integer.MAX_VALUE, start = 0;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0for (int i = 0; i &lt; arr.size(); i++) {\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0sum += arr.get(i);\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0while (sum - arr.get(start) &gt;= target) {\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0sum -= arr.get(start);\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0start++;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (sum &gt;= target) {\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0answer = Math.min(answer, i - start + 1);\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if (answer != Integer.MAX_VALUE) {\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return answer;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return 0;\n\n\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0public static void main(String[] args) {\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ArrayList&lt;Integer&gt; arr = new ArrayList&lt;&gt;();\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0arr.add(2);\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0arr.add(3);\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0arr.add(1);\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0arr.add(4);\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0arr.add(2);\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0arr.add(1);\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0int target = 6;\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0int answer = findMinSubarray(arr, target);\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0System.out.println(answer);\n\n\u00a0\u00a0\u00a0\u00a0}\n\n}<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Python Code<\/strong><\/h3>\n\n\n\n<p>Same approach done in Python<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">def find_min_subarray(arr, target):\n\n\u00a0\u00a0\u00a0\u00a0sum_val = 0\n\n\u00a0\u00a0\u00a0\u00a0answer = float('inf')\n\n\u00a0\u00a0\u00a0\u00a0start = 0\n\n\u00a0\u00a0\u00a0\u00a0for i in range(len(arr)):\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0sum_val += arr[i]\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0while sum_val - arr[start] &gt;= target:\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0sum_val -= arr[start]\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0start += 1\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if sum_val &gt;= target:\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0answer = min(answer, i - start + 1)\n\n\u00a0\u00a0\u00a0\u00a0if answer != float('inf'):\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return answer\n\n\u00a0\u00a0\u00a0\u00a0return 0\n\nif __name__ == &quot;__main__&quot;:\n\n\u00a0\u00a0\u00a0\u00a0arr = [2, 3, 1, 4, 2, 1]\n\n\u00a0\u00a0\u00a0\u00a0target = 6\n\n\u00a0\u00a0\u00a0\u00a0answer = find_min_subarray(arr, target)\n\n\u00a0\u00a0\u00a0\u00a0print(answer)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background has-link-color wp-elements-7f377d353b61ef22b359f2258ab5bacd\" style=\"background-color:#fedcba\"><code>2<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><\/h2>\n\n\n\n<p>The time complexity for the above approach is <strong>O(n).<\/strong> We are just iterating the loop once. The space complexity is O(1) as we are not using any extra space.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In this article, we see various approaches for solving the Minimum Size Subarray Sum problem. It is one of the most interesting questions on <a href=\"https:\/\/leetcode.com\/problems\/minimum-size-subarray-sum\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">LeetCode<\/a>. We have seen that the Sliding Window technique is the most effective method for solving.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the difficult topics in programming is the Sliding Window. It helps you to solve problems in a lower time complexity. This method is useful when we are solving problems related to subarrays or substrings. In this article, we will discuss the problem \u201cMinimum Size Subarray Sum,\u201d which is based on a sliding window. [&hellip;]<\/p>\n","protected":false},"author":14,"featured_media":2829,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jnews-multi-image_gallery":[],"jnews_single_post":null,"jnews_primary_category":{"id":"","hide":""},"footnotes":""},"categories":[7],"tags":[14,15],"class_list":["post-2826","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-structures-algorithms","tag-dsa","tag-leetcode"],"_links":{"self":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/2826","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/comments?post=2826"}],"version-history":[{"count":3,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/2826\/revisions"}],"predecessor-version":[{"id":7387,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/2826\/revisions\/7387"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media\/2829"}],"wp:attachment":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media?parent=2826"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/categories?post=2826"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/tags?post=2826"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}