Kadane's Algorithm — Algorithm Visualization & Coding Challenge

Choose Your Learning Path

How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focus—or scroll down to preview all options.

🧠 Active Learning

Visualize the algorithm step-by-step with interactive animations in real time.

📖 Passive Learning

Read the full explanation, examples, and starter code at your own pace.

🎯 Challenge Mode

Drag and arrange the algorithm steps in the correct execution order.

🧠 Select Active to activate

JUMP INTO VISUALIZATION
Watch algorithms run step by step.

Follow every state change, comparison, and transformation as the execution unfolds in real time.

📖 Select Passive to activate

Understanding Kadane's Algorithm
Detailed explanation and reference materials
Problem Overview

Kadane's Algorithm

Objective: Find the contiguous subarray within a given array that has the maximum sum using Kadane's algorithm.


Problem Description

Kadane's algorithm is a dynamic programming approach that efficiently solves this problem in O(n) time, where n is the number of elements in the array.


Steps to Solve the Problem

1. Initialization:

  • Maintain two variables:
    • max_ending_here: Stores the maximum sum of a contiguous subarray ending at the current index.
    • max_so_far: Stores the overall maximum sum of a contiguous subarray found so far.
  • Both variables are initialized to the first element of the array.

2. Iteration:

  • Loop through the array starting from the second element (index 1).
  • At each index:
    1. Check Current Element:
      • If the current element (arr[i]) is greater than max_ending_here + arr[i], start a new subarray from the current element:
        max_ending_here = arr[i].
      • Otherwise, extend the current subarray:
        max_ending_here = max_ending_here + arr[i].
    2. Update Overall Maximum:
      • Compare max_ending_here with max_so_far.
      • If max_ending_here is larger, update max_so_far:
        max_so_far = max(max_so_far, max_ending_here).

3. Result:

  • After iterating through the entire array, max_so_far contains the maximum sum of a contiguous subarray.

Example

Input Array:
[-2, 1, -3, 4, -1, 2, 1, -5, 4]


Iteration Breakdown:

  • Index 0: Element -2
    • max_ending_here = -2
    • max_so_far = -2
  • Index 1: Element 1
    • max_ending_here = 1
    • max_so_far = 1
  • Index 2: Element -3
    • max_ending_here = -2
    • max_so_far = 1
  • Index 3: Element 4
    • max_ending_here = 4
    • max_so_far = 4
  • Index 4: Element -1
    • max_ending_here = 3
    • max_so_far = 4
  • Index 5: Element 2
    • max_ending_here = 5
    • max_so_far = 5
  • Index 6: Element 1
    • max_ending_here = 6
    • max_so_far = 6
  • Index 7: Element -5
    • max_ending_here = 1
    • max_so_far = 6
  • Index 8: Element 4
    • max_ending_here = 5
    • max_so_far = 6

Final Result:

Maximum Sum of a Contiguous Subarray: 6


Key Insights

  • Kadane's algorithm uses a simple, iterative approach to track the maximum sum, making it efficient and elegant.
  • It dynamically adjusts whether to start a new subarray or continue the current one, based on which yields a higher sum.
— Written by Saurabh Patil • B.Tech CSE • Software Developer

Categories
arrays
java
Reference Link
https://leetcode.com/problems/maximum-subarray/description/

Loading component...

Starter Code
Test, modify, or copy the starter code. Click "Visualize" to import into the canvas.
Java
Output:
Understood Algorithm, Test Me now 🎮

🎯 Select Challenge to activate

🧠 Logic Puzzle
Think & Arrange, Don't Just Copy-Paste

Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.

Arrange the Algorithm Correctly 🧩

The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.

Understand Below Algorithm

Don't Know Current Algorithm ?  

Green text means the instruction is placed in the correct position.

Red text means the instruction is in the wrong position.

Block Colors

Instructions with the same background color indicate particular blocks start and end.

A tick mark means the instruction is correct and locked.

🔒 Locked steps cannot be moved. Only unlocked steps are draggable.

🔊 Enable sound for swap feedback and completion effects.

DrawToCode — Visualize, Practice & Master Algorithms

Learn data structures and algorithms through interactive visualizations. Practice coding problems, track your progress, and understand concepts deeply.

EmailLinkedInTwitterInstagramGitHub
© 2026 DrawToCode. All rights reserved.