🌈 Holi Special – Extra 26% OFF on our Professional Courses | Use Coupon HOLI26OFF    Loading... View Courses →

Kadane's Algorithm  


Introduction
  • Kadane's Algorithm is a method to find the maximum sum of a continuous part of an array.
  • It keeps two things:
    • currentSum — the sum of the current subarray
    • maxSum — the best (maximum) sum found so far
  • It always looks for the best continuous streak of numbers.
  • It moves through the array only once (O(n) time), so it's very fast.
  • Real-World Uses
    • Stock Market Analysis: Finding the best continuous profit period (maximum subarray).
      Kadane's Algorithm Maximum Subarray
  • Working of Kadane's Algorithm:
    Kadane’s Algorithm is like a highly motivated person.
    It always says — keep moving forward.
    If negativity comes in life, don’t stay down.
    Stand up again, start from zero, and continue moving ahead.

    Kadane’s Algorithm keeps adding numbers as long as the total stays positive. If the total becomes negative, it drops it to zero and starts fresh. This helps it find the best possible continuous sum.
    Kadane's Algorithm Motivation