
On a Meta Reels analytics stream, each integer in nums represents the gain or loss in engagement for a consecutive time window. Find the contiguous subarray with the largest possible sum and return that sum.
numsnumsYou must solve this in linear time.
Example 1
nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]6[4, -1, 2, 1] has sum 6, which is the largest possible.Example 2
nums = [1]1[1].Example 3
nums = [5, 4, -1, 7, 8]235 + 4 - 1 + 7 + 8 = 23.1 <= len(nums) <= 10^5-10^4 <= nums[i] <= 10^4nums contains at least one elementnums = [-2, 1, -3, 4, -1, 2, 1, -5, 4]Output6WhyThe maximum-sum contiguous subarray is `[4, -1, 2, 1]`, which sums to `6`.nums = [1]Output1WhyThere is only one possible subarray, so the answer is `1`.nums = [-3, -2, -5]Output-2WhyWhen all values are negative, the best subarray is the single largest element.1 <= len(nums) <= 10^5-10^4 <= nums[i] <= 10^4The subarray must be contiguousAt least one element must be included in the subarraydef max_subarray(nums):