CMD Guide
HomeDSADynamic Programming

Minimum Deletions to Make a Sequence Sorted

The minimum number of deletions needed to make an array sorted equals n − LIS(arr): whatever isn't part of the longest increasing subsequence (LIS) must be removed, because the LIS is exactly the largest set of elements that can stay in place, in order, without violating sortedness.

Recognize the pattern

Brute force → optimal

Brute force: enumerate every subsequence (2ⁿ of them), check if it's sorted, track the longest. Cost: O(2ⁿ · n) time, O(n) space per check — exponential, unusable beyond n≈20.

Optimal: reduce the problem to Longest Increasing Subsequence. Answer = n − LIS_length. Two standard ways to compute LIS:

  1. O(n²) DP — for each index i, dp[i] = length of the longest increasing subsequence ending at i.
  2. O(n log n) patience sorting — maintain a `tails` array of the smallest possible tail value for each achievable subsequence length; binary-search each new element's slot.

Complexity, derived

O(n²) DP: for each of n indices i, scan all j < i (up to n comparisons) to find the best predecessor: dp[i] = 1 + max(dp[j] for j<i, arr[j]<arr[i]). Total comparisons = 1+2+…+(n−1) = n(n−1)/2 → O(n²) time, O(n) space for the dp array.

O(n log n) patience sorting: each of n elements does one binary search over a `tails` array of length ≤ n → n · O(log n) = O(n log n) time, O(n) space for `tails`.

Traced worked example

Array: [4, 2, 3, 6, 10, 1, 12], n = 7. dp[i] = length of LIS ending at index i.

iarr[i]best j<i with arr[j]<arr[i]dp[i]
041
121
23j=1 (2)2
36j=2 (3), dp=23
410j=3 (6), dp=34
511
612j=4 (10), dp=45

max(dp) = 5 → deletions = 7 − 5 = 2 (delete 4 and 1), matching the expected output.

Code (O(n log n))

int minDeletionsToSort(int[] arr) {
    int n = arr.length;
    int[] tails = new int[n];
    int len = 0;
    for (int x : arr) {
        int lo = 0, hi = len;
        while (lo < hi) {              // first index with tails[mid] >= x
            int mid = (lo + hi) / 2;
            if (tails[mid] < x) lo = mid + 1; else hi = mid;
        }
        tails[lo] = x;
        if (lo == len) len++;
    }
    return n - len;                    // deletions = n - LIS length
}

Use strict < comparisons (as above) for a strictly increasing target; switch to <= in the search condition if the target is non-decreasing (duplicates allowed to stay).

Pitfalls

When to use / when not

Read the problem carefully first: this reduction applies only when elements must keep their original relative order. If the problem instead lets you freely reorder, sorting fixes everything and the deletion count is 0 — a classic misread. Use LIS reduction whenever the ask is "minimum deletions to make sorted/increasing" with order preserved — it's optimal and standard. For n ≤ ~2000, the O(n²) DP is simpler to write correctly under interview pressure and has no bugs from binary-search edge cases; prefer O(n log n) patience sorting when n is large (10⁴–10⁶) or when the interviewer explicitly asks for the tightest bound. An alternative framing — longest common subsequence of the array with its sorted copy — also finds the keep-set, but costs O(n²) time and space with no advantage over direct LIS DP, so it's mainly useful pedagogically to show LIS is a special case of LCS.

Takeaways

Recall: Why does the length of the `tails` array at the end equal the LIS length, even though `tails` itself is not a valid subsequence of the input?


Pattern: Longest Increasing Subsequence (patience sorting / O(n²) DP), a classic interleave of DP and binary search covered in CLRS-adjacent interview references and standard LIS treatments (e.g., GeeksforGeeks, competitive programming texts).

🤖 Don't fully get this? Learn it with Claude

Stuck on Minimum Deletions to Make a Sequence Sorted? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Minimum Deletions to Make a Sequence Sorted** (DSA) and want to truly understand it. Explain Minimum Deletions to Make a Sequence Sorted from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Minimum Deletions to Make a Sequence Sorted** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Minimum Deletions to Make a Sequence Sorted** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Minimum Deletions to Make a Sequence Sorted** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes