15 Kaden's Alorithm

Naive Approach

chevron-rightLogichashtag
  1. Generate all subarrays

    1. Calculate the sum

      1. return max sum

chevron-rightCodehashtag
chevron-rightComplexityhashtag

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

Space Complexity: O(1)O(1)

Using Kaden's Algorithm

chevron-rightLogichashtag
  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

chevron-rightCodehashtag
chevron-rightComplexiteshashtag

Time Complexity: O(n)O(n)

Space Complexity: O(1)O(1)

Last updated