<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Balaji R</title>
    <description>The latest articles on DEV Community by Balaji R (@balajiramavathu).</description>
    <link>https://dev.to/balajiramavathu</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F179403%2F704ee5ab-0676-447d-acbf-ee63e60fa68f.png</url>
      <title>DEV Community: Balaji R</title>
      <link>https://dev.to/balajiramavathu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/balajiramavathu"/>
    <language>en</language>
    <item>
      <title>Maximum Subarray: Kadane's Algorithm</title>
      <dc:creator>Balaji R</dc:creator>
      <pubDate>Tue, 02 May 2023 11:27:41 +0000</pubDate>
      <link>https://dev.to/balajiramavathu/coding-patterns-kadanes-algorithm-2blb</link>
      <guid>https://dev.to/balajiramavathu/coding-patterns-kadanes-algorithm-2blb</guid>
      <description>&lt;p&gt;Hey there, folks! Today we're going to talk about Kadane's algorithm, a nifty little trick for finding the maximum subarray sum in an array. If you're a fan of dynamic programming, you're going to love this one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Kadane's Algorithm Explained
&lt;/h2&gt;

&lt;p&gt;So, how does it work? Well, it's pretty simple, actually.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;We start by initializing two variables: &lt;em&gt;maxSoFar&lt;/em&gt; and &lt;em&gt;maxEndingHere&lt;/em&gt;. &lt;em&gt;maxSoFar&lt;/em&gt; keeps track of the maximum subarray sum we've seen so far, and &lt;em&gt;maxEndingHere&lt;/em&gt; keeps track of the maximum subarray sum that ends at the current position in the array.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Then, we loop over the array and do a little dance. For each element in the array, we update &lt;em&gt;maxEndingHere&lt;/em&gt; to be the maximum of the current element and the sum of the previous &lt;em&gt;maxEndingHere&lt;/em&gt; and the current element. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In other words, we're asking ourselves: "is it better to start a new subarray at this element, or to add this element to the subarray that ended at the previous element?"&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Once we've updated &lt;em&gt;maxEndingHere&lt;/em&gt;, we compare it to &lt;em&gt;maxSoFar _and update _maxSoFar&lt;/em&gt; if &lt;em&gt;maxEndingHere&lt;/em&gt; is greater. This means that we've found a new maximum subarray sum.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And that's it! Once we've looped over the entire array, we return &lt;em&gt;maxSoFar&lt;/em&gt; and pat ourselves on the back for a job well done.&lt;/p&gt;

&lt;p&gt;Let's take a look at an example to see how it all fits together. We'll start with this array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We start by setting maxSoFar and maxEndingHere to -2, which is the first element of the array. Then we loop over the rest of the array and do our little dance:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For the second element (1), we update maxEndingHere to be the maximum of 1 and -2 + 1, which is 1.&lt;/li&gt;
&lt;li&gt;For the third element (-3), we update maxEndingHere to be the maximum of -3 and 1 - 3, which is -2.&lt;/li&gt;
&lt;li&gt;For the fourth element (4), we update maxEndingHere to be the maximum of 4 and -2 + 4, which is 4.&lt;/li&gt;
&lt;li&gt;And so on.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At the end of the loop, maxSoFar is 6, which is the maximum subarray sum in the array. Pretty cool, huh?&lt;/p&gt;

&lt;p&gt;Now, you might be wondering: "What if the array has only negative numbers?" Well, fear not, my friend! Kadane's algorithm can handle that too. Let's take a look at this array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nums = [-5, -2, -3, -1, -4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We start by setting &lt;em&gt;maxSoFar&lt;/em&gt; and &lt;em&gt;maxEndingHere&lt;/em&gt; to -5, which is the first element of the array. Then we loop over the rest of the array and do our little dance:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For the second element (-2), we update &lt;em&gt;maxEndingHere&lt;/em&gt; to be the maximum of -2 and -5 - 2, which is -2.&lt;/li&gt;
&lt;li&gt;For the third element (-3), we update &lt;em&gt;maxEndingHere&lt;/em&gt; to be the maximum of -3 and -2 - 3, which is -3.&lt;/li&gt;
&lt;li&gt;And so on
At the end of the loop, &lt;em&gt;maxSoFar&lt;/em&gt; is -1, which is the maximum&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Kotlin Implementation of the algorithm:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun maxSubArray(nums: IntArray): IntArray {
    var maxSoFar = nums[0]
    var maxEndingHere = nums[0]
    var start = 0
    var end = 0
    var newStart = 0
    for (i in 1 until nums.size) {
        if (nums[i] &amp;gt; maxEndingHere + nums[i]) {
            newStart = i
            maxEndingHere = nums[i]
        } else {
            maxEndingHere += nums[i]
        }
        if (maxEndingHere &amp;gt; maxSoFar) {
            maxSoFar = maxEndingHere
            start = newStart
            end = i
        }
    }
    return nums.slice(start..end).toIntArray()
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>competitiveprogamming</category>
      <category>kadanealgorithm</category>
      <category>maximumsubarray</category>
      <category>dynamicprogramming</category>
    </item>
  </channel>
</rss>
