{"id":26776,"date":"2017-12-24T15:31:23","date_gmt":"2017-12-24T10:01:23","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26776"},"modified":"2018-10-31T16:14:02","modified_gmt":"2018-10-31T10:44:02","slug":"python-programming-largest-sum-contiguous-subarray","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/python-programming-largest-sum-contiguous-subarray\/","title":{"rendered":"Largest Sum Contiguous Subarray"},"content":{"rendered":"<p><span style=\"color: #003366;\"><strong>Largest Sum Contiguous Subarray<\/strong><\/span><\/p>\n<p>Write an efficient <a href=\"https:\/\/www.wikitechy.com\/tutorials\/python\/python-program-to-check-leap-year\" target=\"_blank\" rel=\"noopener\">Python program<\/a> to find the sum of contiguous subarray within a one-dimensional <a href=\"https:\/\/www.wikitechy.com\/tutorials\/python\/array-in-python\" target=\"_blank\" rel=\"noopener\">array<\/a> of numbers which has the largest sum.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"alignleft size-full wp-image-26804\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/kadane-Algorithm.png\" alt=\"kadane Algorithm\" width=\"564\" height=\"345\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/kadane-Algorithm.png 564w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/kadane-Algorithm-300x184.png 300w\" sizes=\"(max-width: 564px) 100vw, 564px\" \/><\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<h3 id=\"kadanes-algorithm\"><span style=\"color: #003300;\"><strong>Kadane\u2019s Algorithm:<\/strong><\/span><\/h3>\n<pre>Initialize:\r\n    max_so_far = 0\r\n    max_ending_here = 0\r\n\r\nLoop for each element of the array\r\n  (a) max_ending_here = max_ending_here + a[i]\r\n  (b) if(max_ending_here < 0)\r\n            max_ending_here = 0\r\n  (c) if(max_so_far < max_ending_here)\r\n            max_so_far = max_ending_here\r\nreturn max_so_far\r\n<\/pre>\n[ad type=\u201dbanner\u201d]\n<h3 id=\"explanation\"><span style=\"color: #0000ff;\"><strong>Explanation:<\/strong><\/span><\/h3>\n<p>Simple idea of the <strong>Kadane\u2019s algorithm<\/strong> is to look for all positive contiguous segments of the array (max_ending_here is used for this). And keep track of maximum sum contiguous segment among all positive segments (max_so_far is used for this). Each time we get a positive sum compare it with max_so_far and update max_so_far if it is greater than max_so_far<\/p>\n<pre>    Lets take the example:\r\n    {-2, -3, 4, -1, -2, 1, 5, -3}\r\n\r\n    max_so_far = max_ending_here = 0\r\n\r\n    for i=0,  a[0] =  -2\r\n    max_ending_here = max_ending_here + (-2)\r\n    Set max_ending_here = 0 because max_ending_here < 0\r\n\r\n    for i=1,  a[1] =  -3\r\n    max_ending_here = max_ending_here + (-3)\r\n    Set max_ending_here = 0 because max_ending_here < 0\r\n\r\n    for i=2,  a[2] =  4\r\n    max_ending_here = max_ending_here + (4)\r\n    max_ending_here = 4\r\n    max_so_far is updated to 4 because max_ending_here greater \r\n    than max_so_far which was 0 till now\r\n\r\n    for i=3,  a[3] =  -1\r\n    max_ending_here = max_ending_here + (-1)\r\n    max_ending_here = 3\r\n\r\n    for i=4,  a[4] =  -2\r\n    max_ending_here = max_ending_here + (-2)\r\n    max_ending_here = 1\r\n\r\n    for i=5,  a[5] =  1\r\n    max_ending_here = max_ending_here + (1)\r\n    max_ending_here = 2\r\n\r\n    for i=6,  a[6] =  5\r\n    max_ending_here = max_ending_here + (5)\r\n    max_ending_here = 7\r\n    max_so_far is updated to 7 because max_ending_here is \r\n    greater than max_so_far\r\n\r\n    for i=7,  a[7] =  -3\r\n    max_ending_here = max_ending_here + (-3)\r\n    max_ending_here = 4\r\n<\/pre>\n[ad type=\u201dbanner\u201d]\n<h3 id=\"program-for-largest-sum-contiguous-subarray\"><span style=\"color: #800000;\"><strong>Program<span style=\"color: #800000;\"> for\u00a0Largest Sum Contiguous Subarray<\/span><\/strong><\/span><\/h3>\n[pastacode lang=\u201dpython\u201d manual=\u201d%0A%23%20Python%20program%20to%20find%20maximum%20contiguous%20subarray%20%0A%20%20%20%0A%23%20Function%20to%20find%20the%20maximum%20contiguous%20subarray%20%0Afrom%20sys%20import%20maxint%20%0Adef%20maxSubArraySum(a%2Csize)%3A%20%0A%20%20%20%20%20%20%20%0A%20%20%20%20max_so_far%20%3D%20-maxint%20-%201%0A%20%20%20%20max_ending_here%20%3D%200%0A%20%20%20%20%20%20%20%0A%20%20%20%20for%20i%20in%20range(0%2C%20size)%3A%20%0A%20%20%20%20%20%20%20%20max_ending_here%20%3D%20max_ending_here%20%2B%20a%5Bi%5D%20%0A%20%20%20%20%20%20%20%20if%20(max_so_far%20%3C%20max_ending_here)%3A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20max_so_far%20%3D%20max_ending_here%20%0A%20%20%0A%20%20%20%20%20%20%20%20if%20max_ending_here%20%3C%200%3A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20max_ending_here%20%3D%200%20%20%20%0A%20%20%20%20return%20max_so_far%20%0A%20%20%20%0A%23%20Driver%20function%20to%20check%20the%20above%20function%20%20%0Aa%20%3D%20%5B-13%2C%20-3%2C%20-25%2C%20-20%2C%20-3%2C%20-16%2C%20-23%2C%20-12%2C%20-5%2C%20-22%2C%20-15%2C%20-4%2C%20-7%5D%20%0Aprint%20%22Maximum%20contiguous%20sum%20is%22%2C%20maxSubArraySum(a%2Clen(a))%20\u2033 message=\u201dPython\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<div class=\"responsive-tabs-wrapper\">\n<div class=\"responsive-tabs responsive-tabs--enabled\">\n<div id=\"tablist1-panel1\" class=\"tabcontent responsive-tabs__panel responsive-tabs__panel--active\">\n<h3 id=\"output\"><span style=\"color: #008000;\"><strong>Output :<\/strong><\/span><\/h3>\n<pre>Maximum contiguous sum is -3<\/pre>\n<p>Above program can be optimized further, if we <a href=\"https:\/\/www.wikitechy.com\/view-article\/compare-strings-in-java\" target=\"_blank\" rel=\"noopener\">compare<\/a> <strong>max_so_far<\/strong> with <strong>max_ending_here<\/strong> only if <strong>max_ending_here<\/strong> is greater than 0.<\/p>\n<h3 id=\"program\"><span style=\"color: #800000;\">Program<\/span><\/h3>\n[pastacode lang=\u201dpython\u201d manual=\u201d%0Adef%20maxSubArraySum(a%2Csize)%3A%20%0A%20%20%20%20%20%20%0A%20%20%20%20max_so_far%20%3D%200%0A%20%20%20%20max_ending_here%20%3D%200%0A%20%20%20%20%20%20%0A%20%20%20%20for%20i%20in%20range(0%2C%20size)%3A%20%0A%20%20%20%20%20%20%20%20max_ending_here%20%3D%20max_ending_here%20%2B%20a%5Bi%5D%20%0A%20%20%20%20%20%20%20%20if%20max_ending_here%20%3C%200%3A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20max_ending_here%20%3D%200%0A%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%23%20Do%20not%20compare%20for%20all%20elements.%20Compare%20only%20%20%20%20%0A%20%20%20%20%20%20%20%20%23%20when%20%20max_ending_here%20%3E%200%20%0A%20%20%20%20%20%20%20%20elif%20(max_so_far%20%3C%20max_ending_here)%3A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20max_so_far%20%3D%20max_ending_here%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20return%20max_so_far%20\u2033 message=\u201dPython\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong><span style=\"color: #000080;\">Time Complexity:<\/span> O(n)<\/strong><br \/>\n<span style=\"color: #000080;\"><strong>Algorithmic Paradigm: <\/strong><\/span><a href=\"https:\/\/www.wikitechy.com\/technology\/python-programming-ugly-numbers\/\" target=\"_blank\" rel=\"noopener\">Dynamic Programming<\/a><\/p>\n<p>The implementation handles the case when all numbers in array are negative.<\/p>\n<h3 id=\"program-2\"><span style=\"color: #800000;\">Program<\/span><\/h3>\n[pastacode lang=\u201dpython\u201d manual=\u201d%0A%23%20Python%20program%20to%20find%20maximum%20contiguous%20subarray%20%0A%20%20%0Adef%20maxSubArraySum(a%2Csize)%3A%20%0A%20%20%20%20%20%20%0A%20%20%20%20max_so_far%20%3Da%5B0%5D%20%0A%20%20%20%20curr_max%20%3D%20a%5B0%5D%20%0A%20%20%20%20%20%20%0A%20%20%20%20for%20i%20in%20range(1%2Csize)%3A%20%0A%20%20%20%20%20%20%20%20curr_max%20%3D%20max(a%5Bi%5D%2C%20curr_max%20%2B%20a%5Bi%5D)%20%0A%20%20%20%20%20%20%20%20max_so_far%20%3D%20max(max_so_far%2Ccurr_max)%20%0A%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20return%20max_so_far%20%0A%20%20%0A%23%20Driver%20function%20to%20check%20the%20above%20function%20%20%0Aa%20%3D%20%5B-2%2C%20-3%2C%204%2C%20-1%2C%20-2%2C%201%2C%205%2C%20-3%5D%20%0Aprint%22Maximum%20contiguous%20sum%20is%22%20%2C%20maxSubArraySum(a%2Clen(a))%20\u2033 message=\u201dPython\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h3 id=\"output-2\"><span style=\"color: #008000;\"><strong>Output :<\/strong><\/span><\/h3>\n<pre>Maximum contiguous sum is 7<\/pre>\n<p>To print the subarray with the maximum sum, we maintain indices whenever we get the maximum sum.<\/p>\n<h3 id=\"program-3\"><span style=\"color: #800000;\">Program<\/span><\/h3>\n[pastacode lang=\u201dpython\u201d manual=\u201d%23%20Python%20program%20to%20print%20largest%20contiguous%20array%20sum%20%0A%20%20%0Afrom%20sys%20import%20maxsize%20%0A%20%20%0A%23%20Function%20to%20find%20the%20maximum%20contiguous%20subarray%20%0A%23%20and%20print%20its%20starting%20and%20end%20index%20%0Adef%20maxSubArraySum(a%2Csize)%3A%20%0A%20%20%0A%20%20%20%20max_so_far%20%3D%20-maxsize%20-%201%0A%20%20%20%20max_ending_here%20%3D%200%0A%20%20%20%20start%20%3D%200%0A%20%20%20%20end%20%3D%200%0A%20%20%20%20s%20%3D%200%0A%20%20%0A%20%20%20%20for%20i%20in%20range(0%2Csize)%3A%20%0A%20%20%0A%20%20%20%20%20%20%20%20max_ending_here%20%2B%3D%20a%5Bi%5D%20%0A%20%20%0A%20%20%20%20%20%20%20%20if%20max_so_far%20%3C%20max_ending_here%3A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20max_so_far%20%3D%20max_ending_here%20%0A%20%20%20%20%20%20%20%20%20%20%20%20start%20%3D%20s%20%0A%20%20%20%20%20%20%20%20%20%20%20%20end%20%3D%20i%20%0A%20%20%0A%20%20%20%20%20%20%20%20if%20max_ending_here%20%3C%200%3A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20max_ending_here%20%3D%200%0A%20%20%20%20%20%20%20%20%20%20%20%20s%20%3D%20i%2B1%0A%20%20%0A%20%20%20%20print%20(%22Maximum%20contiguous%20sum%20is%20%25d%22%25(max_so_far))%20%0A%20%20%20%20print%20(%22Starting%20Index%20%25d%22%25(start))%20%0A%20%20%20%20print%20(%22Ending%20Index%20%25d%22%25(end))%20%0A%20%20%0A%23%20Driver%20program%20to%20test%20maxSubArraySum%20%0Aa%20%3D%20%5B-2%2C%20-3%2C%204%2C%20-1%2C%20-2%2C%201%2C%205%2C%20-3%5D%20%0AmaxSubArraySum(a%2Clen(a))%20\u2033 message=\u201dPython\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<h3 id=\"output-3\"><span style=\"color: #008000;\"><strong>Output :<\/strong><\/span><\/h3>\n<pre>Maximum contiguous sum is 7\r\nStarting index 2\r\nEnding index 6<\/pre>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Python Programming &#8211; Largest Sum Contiguous Subarray &#8211; Dynamic Programming Write  program to find the sum of contiguous subarray within one-dimensional array<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,70145],"tags":[79945,73043,72847,79942,72842,72845,70483,72848,72840,72846,72994,72843,72992,72854,72844,72850,72839,79939,79934,79931,79947,79928,78255,79935,79940,79936,79929,79930,79938,77932,79933,79944,79943,72852,79951,78245,79950,72855,79932,79948,79949,78229,72853,72851],"class_list":["post-26776","post","type-post","status-publish","format-standard","hentry","category-coding","category-dynamic-programming","tag-array-find","tag-array-python","tag-concept-of-dynamic-programming","tag-contiguous-sequence","tag-define-dynamic-programming","tag-definition-of-dynamic-programming","tag-dynamic-programming","tag-dynamic-programming-code-generation-algorithm","tag-dynamic-programming-definition","tag-dynamic-programming-in-data-structure","tag-dynamic-programming-in-python","tag-dynamic-programming-problems","tag-dynamic-programming-python","tag-dynamic-programming-set-1","tag-dynamic-programming-software","tag-explain-dynamic-programming","tag-how-to-solve-dynamic-programming-problems","tag-kadane","tag-kadane-algorithm","tag-kadanes-algorithm","tag-kadanes-algorithm-python","tag-largest-sum-contiguous-subarray","tag-max-python","tag-max-subarray","tag-max-subarray-problem","tag-maximum-product-subarray","tag-maximum-subarray","tag-maximum-subarray-problem","tag-maximum-subarray-sum","tag-maximum-subsequence-sum","tag-maximum-sum-subarray","tag-non-contiguous","tag-php-array-sum","tag-problems-on-dynamic-programming","tag-python-find","tag-python-max","tag-python-sum","tag-simple-dynamic-programming-example","tag-subarray","tag-subarray-python","tag-sum-python","tag-sum-sum","tag-types-of-dynamic-programming","tag-youtube-dynamic-programming"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26776","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26776"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26776\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26776"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26776"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26776"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}