Given an integer array nums, find the subarray with the largest sum, and return its sum.
Explanation: The subarray [4,-1,2,1] has the largest sum 6.
Explanation: The subarray [1] has the largest sum 1.
Explanation: The subarray [5,4,-1,7,8] has the largest sum 23.
Follow up: If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
The brute force approach involves checking every possible subarray and computing the sum of each one. This method is straightforward but inefficient for larger arrays due to its high time complexity.
Kadane's Algorithm provides an efficient way of finding the maximum subarray sum in linear time.
currentSum).currentSum drops below zero, reset it to the current element, as a negative currentSum would decrease the sum of any subarray including it.maxSum) across all encountered currentSum values.currentSum and maxSum).