15 Kaden's Alorithm

Naive Approach

Logic
  1. Generate all subarrays

    1. Calculate the sum

      1. return max sum

Code
Complexity

Time Complexity: O(n3)O(n^3)

Space Complexity: O(1)O(1)

Using Kaden's Algorithm

Logic
  1. Calculate the running sum

    1. Update max sum with the running sum

    2. If the running sum is purely negative

    3. the next element is the running sum

    4. else add the next element to the running sum

Code
Complexites

Time Complexity: O(n)O(n)

Space Complexity: O(1)O(1)

Last updated