Longest increasing subsequence: Examples & Code

Algorithm Visualizations November 16, 2026 javascript

Published by Dimitris Kougioumtzis · Updated

For each value, extend the longest strictly increasing subsequence ending at an earlier smaller value.

For each ending position, extend the best earlier subsequence whose final value is strictly smaller.

Open the 3D visualization

Longest increasing subsequence example state preview
Preview of the example's final state. Complete values are available in the worked example and interactive timeline.

When to use Longest increasing subsequence

Order-preserving selections that may skip elements.

Choosing an alternative

A tails-and-binary-search method improves the bound for long sequences.

Worked example: input and state changes

Example input

10, 9, 2, 5, 3, 7, 18

These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.

  1. Step 0: Initial state.

    State at this checkpoint
    {
      "values": [
        1,
        1,
        1,
        1,
        1,
        1,
        1
      ],
      "active": []
    }
  2. Step 6: Input[1]=9, input[3]=5; best length ending at 3 is 1.

    State at this checkpoint
    {
      "values": [
        1,
        1,
        1,
        1,
        1,
        1,
        1
      ],
      "active": [
        1,
        3
      ],
      "input": [
        10,
        9,
        2,
        5,
        3,
        7,
        18
      ]
    }
  3. Step 12: Input[0]=10, input[5]=7; best length ending at 5 is 1.

    State at this checkpoint
    {
      "values": [
        1,
        1,
        1,
        2,
        2,
        1,
        1
      ],
      "active": [
        0,
        5
      ],
      "input": [
        10,
        9,
        2,
        5,
        3,
        7,
        18
      ]
    }
  4. Step 17: Input[0]=10, input[6]=18; best length ending at 6 is 2.

    State at this checkpoint
    {
      "values": [
        1,
        1,
        1,
        2,
        2,
        3,
        2
      ],
      "active": [
        0,
        6
      ],
      "input": [
        10,
        9,
        2,
        5,
        3,
        7,
        18
      ]
    }
  5. Step 23: Longest increasing subsequence length = 4.

    State at this checkpoint
    {
      "values": [
        1,
        1,
        1,
        2,
        2,
        3,
        4
      ],
      "active": [],
      "input": [
        10,
        9,
        2,
        5,
        3,
        7,
        18
      ]
    }

Expected final state

Longest increasing subsequence length = 4.

Complexity and input bounds

Family: Dynamic programming. Time complexity: O(n²).

Space complexity: O(n) lengths and predecessor state.

Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.

2–24 comma-separated integers between 0 and 99.

The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.

Common mistakes and limitations

Strictly increasing means equal values cannot extend the subsequence.

Further reading

For background on algorithm analysis and the JavaScript function interface:

Editorial and testing approach · Browse all algorithm guides

Complete JavaScript implementation

javascript
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// Returns the length of the longest strictly increasing subsequence. Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation.
function algorithm(a, n) {
    let dp = Array(256).fill(0); let best = 0;
    for (let i = 0; i < n; i++) {
        dp[i] = 1;
        for (let j = 0; j < i; j++) {
            if (a[j] < a[i] && dp[j] + 1 > dp[i]) { dp[i] = dp[j] + 1; }
        }
        if (dp[i] > best) { best = dp[i]; }
    }
    return best;
}

Source

Fredman analyzes faster LIS methods. The playground uses the simpler quadratic dynamic-programming recurrence, not the faster algorithm in the paper.