{"id":5425,"date":"2023-08-05T11:00:07","date_gmt":"2023-08-05T11:00:07","guid":{"rendered":"https:\/\/assignmenttask.com\/answers\/?p=5425"},"modified":"2024-01-16T11:12:22","modified_gmt":"2024-01-16T11:12:22","slug":"max-contiguous-subarray","status":"publish","type":"post","link":"https:\/\/assignmenttask.com\/answers\/max-contiguous-subarray\/","title":{"rendered":"Max Contiguous Subarray"},"content":{"rendered":"<h2><strong>Max Contiguous Subarray<\/strong><\/h2>\n<p>&nbsp;<\/p>\n<p>Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list. Input<\/p>\n<p>The input will contain space-separated integers, denoting the elements of the list. Output<\/p>\n<p>The output should be an integer. Explanation<\/p>\n<p>For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be,<\/p>\n<p>[2]<\/p>\n<p>[2, -4]<\/p>\n<p>[2, -4, 5]<\/p>\n<p>[2, -4, 5, -1]<\/p>\n<p>[2, -4, 5, -1, 2]<\/p>\n<p>[2, -4, 5, -1, 2, -3]<\/p>\n<p>[-4]<\/p>\n<p>[-4, 5]<\/p>\n<p>[-4, 5, -1]<\/p>\n<p>[-4, 5, -1, 2]<\/p>\n<p>[-4, 5, -1, 2, -3]<\/p>\n<p>[5]<\/p>\n<p>[5, -1]<\/p>\n<p>[5, -1, 2]<\/p>\n<p>[5, -1, 2, -3]<\/p>\n<p>[-1]<\/p>\n<p>[-1, 2]<\/p>\n<p>[-1, 2, -3]<\/p>\n<p>[2]<\/p>\n<p>[2, -3]<\/p>\n<p>[-3]<\/p>\n<p>Among the above contiguous sub-lists, the contiguous sub-list [5, -1, 2] has the largest sum which is 6.<\/p>\n<p>Sample Input 1<\/p>\n<p>2 -4 5 -1 2 -3<\/p>\n<p>Sample Output 1<\/p>\n<p>6<\/p>\n<p>Sample Input 2<\/p>\n<p>-2 -3 4 -1 -2 1 5 -3<\/p>\n<p>Sample Output 2<\/p>\n<p>7<\/p>\n<h3><strong>Answer:<\/strong><\/h3>\n<p>numbers = [int(item) for item in input().split()]<\/p>\n<p>maxSum = 0<\/p>\n<p>maxTemp = 0<\/p>\n<p>for i in range(0, len(numbers)):<\/p>\n<p>maxTemp += numbers[i]<\/p>\n<p>if maxTemp &lt; 0:<\/p>\n<p>maxTemp = 0<\/p>\n<p>elif (maxSum &lt; maxTemp):<\/p>\n<p>maxSum = maxTemp<\/p>\n<p>print(f'{maxSum}&#8217;)<\/p>\n<p>&nbsp;<\/p>\n<h3 style=\"text-align: center;\"><strong>Looking for Solutions?<\/strong><\/h3>\n<p style=\"text-align: center;\">Get the best assignment assistance from experts.<\/p>\n<p><a href=\"https:\/\/assignmenttask.com\/order-now.php\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-1261 size-full lazyload\" title=\"Order Now\" data-src=\"https:\/\/assignmenttask.com\/answers\/wp-content\/uploads\/2019\/04\/Order-Your-Assignment.jpg\" alt=\"Order-Your-Assignment\" width=\"600\" height=\"150\" data-srcset=\"https:\/\/assignmenttask.com\/answers\/wp-content\/uploads\/2019\/04\/Order-Your-Assignment.jpg 600w, https:\/\/assignmenttask.com\/answers\/wp-content\/uploads\/2019\/04\/Order-Your-Assignment-300x75.jpg 300w, https:\/\/assignmenttask.com\/answers\/wp-content\/uploads\/2019\/04\/Order-Your-Assignment-360x90.jpg 360w\" data-sizes=\"(max-width: 600px) 100vw, 600px\" src=\"data:image\/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==\" style=\"--smush-placeholder-width: 600px; --smush-placeholder-aspect-ratio: 600\/150;\" \/><noscript><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-1261 size-full\" title=\"Order Now\" src=\"https:\/\/assignmenttask.com\/answers\/wp-content\/uploads\/2019\/04\/Order-Your-Assignment.jpg\" alt=\"Order-Your-Assignment\" width=\"600\" height=\"150\" srcset=\"https:\/\/assignmenttask.com\/answers\/wp-content\/uploads\/2019\/04\/Order-Your-Assignment.jpg 600w, https:\/\/assignmenttask.com\/answers\/wp-content\/uploads\/2019\/04\/Order-Your-Assignment-300x75.jpg 300w, https:\/\/assignmenttask.com\/answers\/wp-content\/uploads\/2019\/04\/Order-Your-Assignment-360x90.jpg 360w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/noscript><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Max Contiguous Subarray &nbsp; Given a list of integers, write a program to identify the contiguous sub-list that has the largest sum and print the sum. Any non-empty slice of the list with step size 1 can be considered as a contiguous sub-list. Input The input will contain space-separated integers, denoting the elements of the list. Output The output should be an integer. Explanation For example, if the given list is [2, -4, 5, -1, 2, -3], then all the possible contiguous sub-lists will be, [2] [2, -4] [2, -4, 5] [2, -4, 5, -1] [2, -4, 5, -1, 2] <a href=\"https:\/\/assignmenttask.com\/answers\/max-contiguous-subarray\/\" class=\"read-more\">Read More &#8230;<\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[729],"tags":[],"_links":{"self":[{"href":"https:\/\/assignmenttask.com\/answers\/wp-json\/wp\/v2\/posts\/5425"}],"collection":[{"href":"https:\/\/assignmenttask.com\/answers\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/assignmenttask.com\/answers\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/assignmenttask.com\/answers\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/assignmenttask.com\/answers\/wp-json\/wp\/v2\/comments?post=5425"}],"version-history":[{"count":1,"href":"https:\/\/assignmenttask.com\/answers\/wp-json\/wp\/v2\/posts\/5425\/revisions"}],"predecessor-version":[{"id":5427,"href":"https:\/\/assignmenttask.com\/answers\/wp-json\/wp\/v2\/posts\/5425\/revisions\/5427"}],"wp:attachment":[{"href":"https:\/\/assignmenttask.com\/answers\/wp-json\/wp\/v2\/media?parent=5425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/assignmenttask.com\/answers\/wp-json\/wp\/v2\/categories?post=5425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/assignmenttask.com\/answers\/wp-json\/wp\/v2\/tags?post=5425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}