potential On solution to Longest Increasing Subsequence
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
For the general Longest Increasing Subsequence problem, O(n) is usually the wrong target. The standard optimal solution in the comparison model is O(n log n), and that is the result most people should aim to understand and implement.
That does not mean linear-time variants never exist. It means they require extra assumptions, such as bounded value ranges or specialized input structure. For arbitrary integer sequences, a true general O(n) LIS algorithm is not the normal answer.
The Problem Statement
Given an array, the LIS length is the longest subsequence whose values are strictly increasing while preserving original order. A subsequence is not required to be contiguous.
Example:
One LIS is [2, 3, 7, 18], so the answer is 4.
The Classic O(n^2) Dynamic Programming Solution
The direct dynamic programming idea is:
- '
dp[i]= LIS length ending at indexi' - for each
i, check all earlierj < i - if
arr[j] < arr[i], try extendingdp[j]
This is easy to understand and perfectly fine for moderate input sizes. It is just not optimal.
The Standard O(n log n) Solution
The faster approach uses a tails array plus binary search. tails[k] stores the smallest possible tail value of an increasing subsequence of length k + 1.
This is the algorithm most interviewers and competitive programmers expect. It is fast, elegant, and broadly applicable.
Why General O(n) Is Not the Usual Answer
The key obstacle is that LIS needs order information across the sequence. In the general case, each new value may need to be compared against a dynamically changing frontier of best subsequence tails. That is why binary search appears naturally and why log n is hard to remove without additional assumptions.
If someone proposes a general O(n) algorithm for arbitrary inputs, the natural question is: what special structure is being exploited? Without extra structure, the usual answer remains O(n log n).
When Near-Linear Variants Can Exist
If values come from a small bounded range, data structures such as Fenwick trees or segment trees can make the complexity depend on value range instead of plain n. For example, a DP over compressed coordinates can run in O(n log V), where V is the number of distinct values.
That can feel close to linear if V is small, but it is not the same as a general unrestricted O(n) solution.
Similarly, if the input has special structure such as already sorted blocks or limited disorder, a tailored algorithm may do better in practice. Those are special cases, not the generic LIS result.
Common Pitfalls
Confusing subsequence with substring
LIS is about preserving order, not contiguity. If your algorithm only checks contiguous segments, you are solving a different problem.
Thinking tails stores the actual LIS
In the O(n log n) algorithm, tails helps compute the length, but it does not directly store a valid subsequence. Reconstructing the actual LIS requires parent pointers or additional bookkeeping.
Claiming O(n) after adding a tree structure
Fenwick trees and segment trees still cost logarithmic time per update or query. They are excellent tools, but they do not magically make the general problem linear.
Using the wrong binary search variant
For strictly increasing subsequences, bisect_left is usually correct. For non-decreasing variants, the search rule changes.
Summary
- The general LIS problem is not usually solved in
O(n). - The simple DP solution is
O(n^2). - The standard optimal comparison-based solution is
O(n log n)using atailsarray and binary search. - Faster-looking variants need extra assumptions such as bounded value ranges or special input structure.
- If the goal is the general interview or production answer, learn the
O(n log n)method first.
Related reading
- Practical Uses of Fractals in Programming
- Pre-order to post-order traversal
- Predicting Values with k-Means Clustering Algorithm
- Predictive blood glucose algorithm?
- Prefix search against half a billion strings
- Prefix sums weighted by a polynomial expression, can you do faster?
- Practical rules for premature optimization
- Pre pulling docker images in AMI to reduce node and pod fresh start time slows down it's execution when using nvidia-docker with GPU enabled pods

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.