Explanation
- Kadane’s Algorithm is used to find the **maximum sum subarray** in a given array of integers. It works in **O(n)** time.
-
-
Steps
- Initialize two variables:
current_sumandmax_sum. - Traverse through the array, adding each element to
current_sum. - If
current_sumbecomes negative, reset it to 0. - Update
max_sumto the maximum value betweenmax_sumandcurrent_sum.
- Initialize two variables:
-
Time Complexity
- O(n), where
nis the number of elements in the array.
- O(n), where