Largest Sum Contiguous Subarray
# Introduction to Largest Sum Contiguous Subarray
The Largest Sum Contiguous Subarray problem is an important algorithmic problem that deals with finding the largest sum of all possible contiguous subarrays within an array. This problem can be seen as a special case of the maximum subarray problem, which is the task of finding the subarray with the largest sum within a given array. In this article, we will discuss how to solve this problem using the Kadane's Algorithm, which is the most efficient algorithm for solving this problem. We will also discuss other algorithms for solving this problem and their respective time complexities.
Worried About Failing Tech Interviews?
Attend our webinar on
"How to nail your next tech interview" and learn
.png)
Hosted By
Ryan Valles
Founder, Interview Kickstart

Our tried & tested strategy for cracking interviews

How FAANG hiring process works

The 4 areas you must prepare for

How you can accelerate your learnings
Register for Webinar
The Largest Sum Contiguous Subarray algorithm is a dynamic programming algorithm that allows for the identification of the contiguous subarray with the largest sum within a given array. The algorithm works by first setting the current maximum sum to the first element in the array and the current subarray to the first element in the array. It then iterates through the rest of the array, calculating the sum of the current subarray and the current element in the array. If the sum is greater than the current maximum sum, the current maximum sum is updated and the current subarray is set to the subarray that includes the current element. If the sum is less than or equal to the current maximum sum, the current subarray is set to just the current element. This process is repeated until the end of the array is reached, and the maximum sum and the subarray that produces that sum is returned.
Sample Code:
```
# Computes the largest sum contiguous subarray
# of the given array
#
# @param array: The array to search
# @return: A tuple containing the maximum sum
# and the subarray that produces that sum
def largestSumContiguousSubarray(array):
# Set the current maximum sum to the first
# element in the array
max_sum = array[0]
# Set the current subarray to the first
# element in the array
curr_subarray = [array[0]]
# Iterate through the rest of the array
for i in range(1, len(array)):
# Calculate the sum of the current subarray
# and the current element
curr_sum = sum(curr_subarray) + array[i]
# If the sum is greater than the current maximum sum
if curr_sum > max_sum:
# Update the current maximum sum
max_sum = curr_sum
# Set the current subarray to the subarray
# that includes the current element
curr_subarray = curr_subarray + [array[i]]
# If the sum is less than or equal to the
# current maximum sum
else:
# Set the current subarray to just the
# current element
curr_subarray = [array[i]]
# Return the maximum sum and the subarray
# that produces that sum
return (max_sum, curr_subarray)
```