Find the Largest Contiguous Subarray
Description
Hey there, young coder! Are you ready to embark on a fun journey to find the Largest Contiguous Subarray? Imagine you have a list of numbers, and your job is to find the biggest total sum you can get by adding consecutive numbers from the list. It's like finding a hidden treasure of the biggest sum!
Here's what you'll do: You'll write a function that takes a list of numbers, and you'll figure out which part of the list adds up to the largest sum possible. You'll need to return that largest sum.
For example, if you have the list [2, -1, 3, -4, 5, -1, 2], the largest sum of a contiguous subarray is from [5, -1, 2] where the sum is 6.
Examples:
find_largest_contiguous_subarray([1, -2, 3, 5, -1]) # Returns: 8, because the subarray [3, 5] gives the largest sum find_largest_contiguous_subarray([-1, -2, -3, -4]) # Returns: -1, since all numbers are negative, the largest single number is -1 find_largest_contiguous_subarray([4, -1, 2, 1]) # Returns: 6, because the subarray [4, -1, 2, 1] gives the largest sum
Have fun finding the treasure!
Hint: You might want to track your current subarray sum and update it as you go along. If your current subarray sum is negative, consider starting a new subarray at the next number.
Inputs
Arg 1:
[1,-2,3,5,-1]Expected Output
8Actual Output