Free AI web copilot to create summaries, insights and extended knowledge, download it at here
2872
Abstract
span> <span class="hljs-keyword">with</span> maximum <span class="hljs-built_in">sum</span> <span class="hljs-keyword">is</span> [<span class="hljs-number">2</span> <span class="hljs-number">3</span>].</pre></div><p id="db1d">Let's see the brute force approach now.</p><h1 id="1678">Brute Force Approach</h1><p id="9426">The crux of this approach is that we will look at every <b>K</b> sized subarray and choose the subarray with the maximum sum.</p><h2 id="de79">Here's the algorithm:</h2><ol><li>Use a loop to iterate through the input array, and for each index, find the sum of the next K elements.</li><li>Save the highest sum you encounter during the traversal.</li></ol><figure id="2175"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*wHcRSUfsbURQ2_H6BEf9mQ.gif"><figcaption></figcaption></figure><p id="bccf">The code for this approach is straightforward.</p><h1 id="825b">Code</h1>
<figure id="67fa">
<div>
<div>
<iframe class="gist-iframe" src="/gist/PrasadGanesh/bd760eb94cdbcbb70a40e3e3b2b013d0.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><div id="43a4"><pre><span class="hljs-attribute">Output</span>:
<span class="hljs-attribute">3</span> <span class="hljs-number">9</span> <span class="hljs-number">5</span>
<span class="hljs-attribute">17</span></pre></div><p id="7fd6"><b>Explanation</b>: The function <b>maximum_sum_subarray_k</b> accepts an array as well as an integer k. It uses a for loop (line 16) that starts from the index 0 until the last index N - K. It uses another nested loop (line 20) to count the sum of the next K elements from the current index.</p><p id="8558">Line 27 checks if the sum of the current subarray is greater than the maximum sum being maintained so far. If yes, it updates the result (line 29)and saves the resultant subarray's starting index (line 31). The driver function <b>main</b> is used to test the function.</p><h1 id="7f96">Time & Space Complexity</h1><p id="918c"><b>Time</b>: The time complexity is O(NK), where N is the size of the input array and K is the size of the subarray. The outer loop runs for N times, and the nested inner loop runs K times.</p><p id="9628"><b>Space</b>: Since this approach does not need extra space, space complexity is O(1).</p><h1 id="a358">Efficient Approach — Sliding window</h1><p id="af49">Before explaining how the sliding window works, look at this animation, which shows the sum of each window of size 3.</p><figure id="8b37"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*24cLCQxyAeNpUxbZkBd9SQ.gif"><figcaption>sliding a window of size 3</figcaption></figure><p id="8d57">Notice that the sum of each window is calculated using the sum of the pre
Options
vious window and applying these two operations.</p><ol><li>When the window is sliding, one element goes out of the window. Hence we subtract that element from the sum.</li><li>The next element becomes part of the window. Hence we add that element to the sum.</li></ol><figure id="b940"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*YSRm06LeY1nmJVCmcQenhg.gif"><figcaption></figcaption></figure><p id="13b5">This algorithm needs only a single iteration of the array, making it linear 🥳. Unlike the brute force approach, we are not repeatedly calculating the sum of subarrays that already have been computed but using only two constant operations (addition and subtraction).</p><p id="8ccb">Now, let's see the fun part.</p><h1 id="e30a">Code</h1>
<figure id="1aa8">
<div>
<div>
<iframe class="gist-iframe" src="/gist/PrasadGanesh/793c91dee2ba3cce460299c5ef7eec5c.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><div id="2b30"><pre><span class="hljs-symbol">Output:</span>
<span class="hljs-number">17</span></pre></div><p id="3b4e"><b>Explanation</b>: On lines 12 to 15, we compute the sum of the first K elements of the array so that we can use it to calculate the sum of the next subarray. Another loop traverses the array from index k to the end (line 19).</p><p id="a52c">On line 22, we add the kth element (that becomes part of the sliding windows) and subtract the (i - k)th element (the first element of the previous subarray). Finally, we update the result if the current subarray sum is greater than our maximum sum so far.</p><h1 id="c102">Time & Space Complexity</h1><p id="4457"><b>Time</b>: Since we are only doing a single array iteration, the time complexity is O(n).</p><p id="929d"><b>Space</b>: We are not using any extra space; hence space complexity is constant.</p><p id="b032">If you like this article, please give it a clap 👏.</p><div id="38b0" class="link-block">
<a href="https://ganeshpr227.medium.com/30-days-interview-preparation-plan-200-best-coding-questions-and-behavioural-interviews-3f8fc19c2361">
<div>
<div>
<h2>30 Days Interview Preparation Plan: 200+ best coding questions and Behavioural interviews</h2>
<div><h3>I have collected these questions from several excellent sources like geeksforgeeks, leetcode, interview experiences …</h3></div>
<div><p>ganeshpr227.medium.com</p></div>
</div>
<div>
<div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*Oo5tZzjboKfxQVAbnOFSXg.png)"></div>
</div>
</div>
</a>
</div></article></body>