{"id":1846,"date":"2021-09-14T17:50:38","date_gmt":"2021-09-14T12:20:38","guid":{"rendered":"https:\/\/www.interviewbit.com\/blog\/?p=1846"},"modified":"2022-06-15T11:54:09","modified_gmt":"2022-06-15T06:24:09","slug":"maximum-subarray-sum","status":"publish","type":"post","link":"https:\/\/www.interviewbit.com\/blog\/maximum-subarray-sum\/","title":{"rendered":"Maximum Subarray Sum: Kadane&#8217;s Algorithm"},"content":{"rendered":"\n<div class=\"gutentoc tocactive ullist\"><div class=\"gutentoc-toc-wrap\"><div class=\"gutentoc-toc-title-wrap\"><div class=\"gutentoc-toc-title\">Table Of Contents<\/div><div id=\"open\" class=\"text_open\">show<\/div><\/div><div id=\"toclist\"><div class=\"gutentoc-toc__list-wrap\"><ul class=\"gutentoc-toc__list\"><li><a href=\"#problem-statement\">Problem Statement<\/a><\/li><li><a href=\"#simple-approach\">Simple Approach:<\/a><\/li><ul class=\"gutentoc-toc__list\"><li><a href=\"#c-implementation\">C implementation<\/a><\/li><li><a href=\"#c-implementation\">C++ implementation<\/a><\/li><li><a href=\"#java-implementation\">Java implementation<\/a><\/li><li><a href=\"#python-implementation\">Python implementation<\/a><\/li><\/ul><li><a href=\"#efficient-approach-kadanes-algorithm\">Efficient Approach: Kadane&#8217;s Algorithm<\/a><\/li><ul class=\"gutentoc-toc__list\"><li><a href=\"#c-implementation-of-efficient-approach\">C implementation of Efficient approach<\/a><\/li><li><a href=\"#c-implementation-of-efficient-approach\">C++ implementation of Efficient approach<\/a><\/li><li><a href=\"#java-implementation-of-efficient-approach\">Java implementation of Efficient approach<\/a><\/li><li><a href=\"#python-implementation-of-efficient-approach\">Python implementation of Efficient approach<\/a><\/li><\/ul><li><a href=\"#practice-problems--\">Practice Problems &#8211;<\/a><\/li><li><a href=\"#frequently-asked-questions\">Frequently Asked Questions<\/a><\/li><\/ul><\/div><\/div><\/div><\/div>\n\n\n\n<h2 id=\"problem-statement\">Problem Statement<\/h2>\n\n\n\n<p><strong>Subarrays<\/strong> are arrays inside another array which only contains contiguous elements.<\/p>\n\n\n\n<p>Given an array of integers, the task is to find the maximum subarray sum possible of all the non-empty subarrays.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img  loading=\"lazy\"  src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAP+KeNJXAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=\"  alt=\"Array\"  class=\"wp-image-1848 pk-lazyload\"  width=\"565\"  height=\"259\"  data-pk-sizes=\"auto\"  data-ls-sizes=\"(max-width: 565px) 100vw, 565px\"  data-pk-src=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/09\/Array.jpg\"  data-pk-srcset=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/09\/Array.jpg 676w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/09\/Array-300x138.jpg 300w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/09\/Array-150x69.jpg 150w\" ><\/figure><\/div>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p><strong>Input:<\/strong> [-3, -4, 5, -1, 2, -4, 6, -1]<br><strong>Output:<\/strong> 8<br><strong>Explanation:<\/strong> Subarray [5, -1, 2, -4, 6] is the max sum contiguous subarray with sum 8.<\/p>\n\n\n\n<p><strong>Input:<\/strong> [-2, 3, -1, 2]<br><strong>Output:<\/strong> 4<br><strong>Explanation:<\/strong> Subarray [3, -1, 2] is the max sum contiguous subarray with sum 4.<\/p>\n\n\n\n<p>We would be solving the problem by following approaches &#8211;<\/p>\n\n\n\n<ul><li>Simple approach<\/li><li>Efficient Approach: Kadane&#8217;s Algorithm<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator is-style-wide\"\/>\n\n\n\n<h2 id=\"simple-approach\">Simple Approach:<\/h2>\n\n\n\n<p>The simple approach to solve this problem is to run two for loops and for every subarray check if it is the maximum sum possible. Follow the below steps to solve the problem.<\/p>\n\n\n\n<ul><li>Run a loop for i from 0 to n &#8211; 1, where n is the size of the array.<\/li><li>Now, we will run a nested loop for j from i to n &#8211; 1 and add the value of the element at index j to a variable currentMax.<\/li><li>Lastly, for every subarray, we will check if the currentMax is the maximum sum of all contiguous subarrays.<\/li><\/ul>\n\n\n\n<h3 id=\"c-implementation\">C implementation<\/h3>\n\n\n\n<iframe style='max-width:100%; border: none; height: 375px; width: {width}px;' height=375 width=700 src=https:\/\/www.interviewbit.com\/embed\/snippet\/00c43b845b8ff892dc80 title='Interviewbit Ide snippet\/00c43b845b8ff892dc80' loading=\"lazy\" allow=\"clipboard-write\" allowfullscreen referrerpolicy=\"unsafe-url\" sandbox=\"allow-scripts allow-same-origin allow-popups allow-top-navigation-by-user-activation allow-popups-to-escape-sandbox\"><\/iframe>\n\n\n\n<h3 id=\"c-implementation\"><span id=\"c-implementation-2\">C++ implementation<\/span><\/h3>\n\n\n\n<iframe style='max-width:100%; border: none; height: 375px; width: {width}px;' height=375 width=700 src=https:\/\/www.interviewbit.com\/embed\/snippet\/3a127863d2ade79a50bf title='Interviewbit Ide snippet\/3a127863d2ade79a50bf' loading=\"lazy\" allow=\"clipboard-write\" allowfullscreen referrerpolicy=\"unsafe-url\" sandbox=\"allow-same-origin allow-scripts allow-popups allow-top-navigation-by-user-activation allow-popups-to-escape-sandbox\"><\/iframe>\n\n\n\n<h3 id=\"java-implementation\">Java implementation<\/h3>\n\n\n\n<iframe style='max-width:100%; border: none; height: 375px; width: {width}px;' height=375 width=700 src=https:\/\/www.interviewbit.com\/embed\/snippet\/19e908ec433c7278981d title='Interviewbit Ide snippet\/19e908ec433c7278981d' loading=\"lazy\" allow=\"clipboard-write\" allowfullscreen referrerpolicy=\"unsafe-url\" sandbox=\"allow-same-origin allow-scripts allow-popups allow-top-navigation-by-user-activation allow-popups-to-escape-sandbox\"><\/iframe>\n\n\n\n<h3 id=\"python-implementation\">Python implementation<\/h3>\n\n\n\n<iframe style='max-width:100%; border: none; height: 375px; width: {width}px;' height=375 width=700 src=https:\/\/www.interviewbit.com\/embed\/snippet\/83877754ef7b93f1690b title='Interviewbit Ide snippet\/83877754ef7b93f1690b' loading=\"lazy\" allow=\"clipboard-write\" allowfullscreen referrerpolicy=\"unsafe-url\" sandbox=\"allow-same-origin allow-scripts allow-popups allow-top-navigation-by-user-activation allow-popups-to-escape-sandbox\"><\/iframe>\n\n\n\n<p><strong>Time complexity:<\/strong> O(N^2), Where N is the size of the array.<br><strong>Space complexity:<\/strong> O(1)<\/p>\n\n\n\n<hr class=\"wp-block-separator is-style-wide\"\/>\n\n\n\n<h2 id=\"efficient-approach-kadanes-algorithm\">Efficient Approach: Kadane&#8217;s Algorithm<\/h2>\n\n\n\n<p><strong>Kadane\u2019s Algorithm<\/strong> is an iterative <a href=\"https:\/\/www.interviewbit.com\/courses\/programming\/dynamic-programming\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>dynamic programming<\/strong><\/a> algorithm. It calculates the maximum sum subarray ending at a particular position by using the maximum sum subarray ending at the previous position. Follow the below steps to solve the problem.<\/p>\n\n\n\n<ul><li>Define two-variable currSum which stores maximum sum ending here and maxSum which stores maximum sum so far.<\/li><li>Initialize currSum with 0 and maxSum with INT_MIN.<\/li><li>Now, iterate over the array and add the value of the current element to currSum and check<ul><li>If currSum is greater than maxSum, update maxSum equals to currSum.<\/li><li>If currSum is less than zero, make currSum equal to zero.<\/li><\/ul><\/li><li>Finally, print the value of maxSum.<\/li><\/ul>\n\n\n\n<p><strong>Dry run of the above approach<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img  loading=\"lazy\"  src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAP+KeNJXAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=\"  alt=\"Dry Run Of Maximum Sub Array Sum\"  class=\"wp-image-1858 pk-lazyload\"  width=\"402\"  height=\"630\"  data-pk-sizes=\"auto\"  data-ls-sizes=\"(max-width: 402px) 100vw, 402px\"  data-pk-src=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/09\/Dry-run-of-array-sum.jpg\"  data-pk-srcset=\"https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/09\/Dry-run-of-array-sum.jpg 559w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/09\/Dry-run-of-array-sum-191x300.jpg 191w, https:\/\/www.interviewbit.com\/blog\/wp-content\/uploads\/2021\/09\/Dry-run-of-array-sum-150x235.jpg 150w\" ><\/figure><\/div>\n\n\n\n<h3 id=\"c-implementation-of-efficient-approach\">C implementation of Efficient approach<\/h3>\n\n\n\n<iframe style='max-width:100%; border: none; height: 375px; width: {width}px;' height=375 width=700 src=https:\/\/www.interviewbit.com\/embed\/snippet\/50fac46813f9f477a9c5 title='Interviewbit Ide snippet\/50fac46813f9f477a9c5' loading=\"lazy\" allow=\"clipboard-write\" allowfullscreen referrerpolicy=\"unsafe-url\" sandbox=\"allow-same-origin allow-scripts allow-popups allow-top-navigation-by-user-activation allow-popups-to-escape-sandbox\"><\/iframe>\n\n\n\n<h3 id=\"c-implementation-of-efficient-approach\"><span id=\"c-implementation-of-efficient-approach-2\">C++ implementation of Efficient approach<\/span><\/h3>\n\n\n\n<iframe style='max-width:100%; border: none; height: 375px; width: {width}px;' height=375 width=700 src=https:\/\/www.interviewbit.com\/embed\/snippet\/c3f04c41879189d78002 title='Interviewbit Ide snippet\/c3f04c41879189d78002' loading=\"lazy\" allow=\"clipboard-write\" allowfullscreen referrerpolicy=\"unsafe-url\" sandbox=\"allow-same-origin allow-scripts allow-popups allow-top-navigation-by-user-activation allow-popups-to-escape-sandbox\"><\/iframe>\n\n\n\n<h3 id=\"java-implementation-of-efficient-approach\">Java implementation of Efficient approach<\/h3>\n\n\n\n<iframe style='max-width:100%; border: none; height: 375px; width: {width}px;' height=375 width=700 src=https:\/\/www.interviewbit.com\/embed\/snippet\/a106f1115cfc842d2077 title='Interviewbit Ide snippet\/a106f1115cfc842d2077' loading=\"lazy\" allow=\"clipboard-write\" allowfullscreen referrerpolicy=\"unsafe-url\" sandbox=\"allow-same-origin allow-scripts allow-popups allow-top-navigation-by-user-activation allow-popups-to-escape-sandbox\"><\/iframe>\n\n\n\n<h3 id=\"python-implementation-of-efficient-approach\">Python implementation of Efficient approach<\/h3>\n\n\n\n<iframe style='max-width:100%; border: none; height: 375px; width: {width}px;' height=375 width=700 src=https:\/\/www.interviewbit.com\/embed\/snippet\/b3e5eafa9d8a45ae6c14 title='Interviewbit Ide snippet\/b3e5eafa9d8a45ae6c14' loading=\"lazy\" allow=\"clipboard-write\" allowfullscreen referrerpolicy=\"unsafe-url\" sandbox=\"allow-same-origin allow-scripts allow-popups allow-top-navigation-by-user-activation allow-popups-to-escape-sandbox\"><\/iframe>\n\n\n\n<p><strong>Time complexity:<\/strong> O(N), Where N is the size of the array.<br><strong>Space complexity:<\/strong> O(1)<\/p>\n\n\n\n<hr class=\"wp-block-separator is-style-wide\"\/>\n\n\n\n<h2 id=\"practice-problems--\"><span id=\"practice-problems\">Practice Problems &#8211;<\/span><\/h2>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/www.interviewbit.com\/problems\/flip\/\" target=\"_blank\">Flip Problem<\/a><br><a rel=\"noreferrer noopener\" href=\"https:\/\/www.interviewbit.com\/problems\/max-product-subarray\/\" target=\"_blank\">Maximum Product Subarray<\/a><br><a href=\"https:\/\/www.interviewbit.com\/problems\/max-sum-contiguous-subarray\/\" target=\"_blank\" rel=\"noreferrer noopener\">Maximum Sum Contiguous Subarray<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator is-style-wide\"\/>\n\n\n\n<h2 id=\"frequently-asked-questions\">Frequently Asked Questions<\/h2>\n\n\n\n<p id=\"is-kadane\u2019s-algorithm-dynamic-programming\"><strong>Q. Is Kadane\u2019s algorithm Dynamic Programming?<\/strong><br>A. Yes, It is an iterative dynamic programming algorithm.<\/p>\n\n\n\n<p id=\"what-should-be-the-maximum-subarray-sum-if-all-the-elements-of-the-array-are-negative\"><strong>Q. What should be the maximum subarray sum if all the elements of the array are negative?<\/strong><br>A. It depends if we are considering empty subarray or not. If we consider an empty subarray then the output should be 0 else, the output should be the maximum element of the array(the element closest to 0).<\/p>\n\n\n\n<p id=\"what-is-the-time-complexity-of-kadane\u2019s-algorithm\"><strong>Q. What is the time complexity of Kadane\u2019s algorithm?<\/strong><br>A. The time complexity of Kadane\u2019s algorithm is O(N) where N is the size of the array.<\/p>\n","protected":false},"excerpt":{"rendered":"Problem Statement Subarrays are arrays inside another array which only contains contiguous elements. Given an array of integers,&hellip;\n","protected":false},"author":5,"featured_media":1851,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_daextam_enable_autolinks":"1","csco_singular_sidebar":"","csco_page_header_type":"","csco_appearance_grid":"","csco_page_load_nextpost":"","csco_post_video_location":[],"csco_post_video_location_hash":"","csco_post_video_url":"","csco_post_video_bg_start_time":0,"csco_post_video_bg_end_time":0},"categories":[145],"tags":[144,142,143],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/posts\/1846"}],"collection":[{"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/comments?post=1846"}],"version-history":[{"count":107,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/posts\/1846\/revisions"}],"predecessor-version":[{"id":9826,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/posts\/1846\/revisions\/9826"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/media\/1851"}],"wp:attachment":[{"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/media?parent=1846"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/categories?post=1846"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.interviewbit.com\/blog\/wp-json\/wp\/v2\/tags?post=1846"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}