Minimum number of increasing subsequences
Last Updated :
11 Jul, 2025
Given an array of integers of size N, you have to divide it into the minimum number of "strictly increasing subsequences"
For example: let the sequence be {1, 3, 2, 4}, then the answer would be 2. In this case, the first increasing sequence would be {1, 3, 4} and the second would be {2}.
Examples:
Input : arr[] = {1, 3, 2, 4}
Output: 2
There are two increasing subsequences {1, 3, 4} and {2}
Input : arr[] = {4, 3, 2, 1}
Output : 4
Input : arr[] = {1, 2, 3, 4}
Output : 1
Input : arr[] = {1, 6, 2, 4, 3}
Output : 3
Approach: To solve the problem, follow the below idea:
If we focus on the examples, we can see that the Minimum number of increasing subsequences equals to the length of longest decreasing subsequence where each element from the longest decreasing subsequence belongs to a single increasing subsequence, so it can be found in N*Log(N) time complexity in the same way as longest increasing subsequence by multiplying all the elements with -1.
Step-by-step approach:
- Initialize a dp[] array of length N.
- Multiply all the elements of the array with -1, so that our problem now gets reduced to finding the length of longest increasing subsequence.
- Return the length of longest increasing subsequence using Binary Search.
Below is the implementation of above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// To search for correct position of num in array dp
int search(vector<int> dp, int num)
{
// Initialise low,high and ans
int low = 0, high = dp.size() - 1;
int ans = -1;
while (low <= high) {
// Get mid
int mid = low + ((high - low) / 2);
// If mid element is >=num search for left half
if (dp[mid] >= num) {
ans = mid;
high = mid - 1;
}
else
low = mid + 1;
}
return ans;
}
int longestIncreasingSubsequence(vector<int> arr, int N)
{
vector<int> ans;
// Initialize the answer vector with the
// first element of nums
ans.push_back(arr[0]);
for (int i = 1; i < N; i++) {
if (arr[i] > ans.back()) {
// If the current number is greater
// than the last element of the answer
// vector, it means we have found a
// longer increasing subsequence.
// Hence, we append the current number
// to the answer vector.
ans.push_back(arr[i]);
}
else {
// If the current number is not
// greater than the last element of
// the answer vector, we perform
// a binary search to find the smallest
// element in the answer vector that
// is greater than or equal to the
// current number.
// The lower_bound function returns
// an iterator pointing to the first
// element that is not less than
// the current number.
int low = lower_bound(ans.begin(), ans.end(),
arr[i])
- ans.begin();
// We update the element at the
// found position with the current number.
// By doing this, we are maintaining
// a sorted order in the answer vector.
ans[low] = arr[i];
}
}
// The length of the answer vector
// represents the length of the
// longest increasing subsequence.
return ans.size();
}
// Driver code
int main()
{
int N = 5;
vector<int> arr{1, 6, 2, 4, 3};
for (int i = 0; i < N; i++)
arr[i] = -arr[i];
cout << longestIncreasingSubsequence(arr, N) << endl;
return 0;
}
// This code is contributed by shinjanpatra
Java
import java.util.*;
class Main {
// To search for correct position of num in array dp
static int search(ArrayList<Integer> dp, int num) {
// Initialise low, high and ans
int low = 0, high = dp.size() - 1;
int ans = -1;
while (low <= high) {
// Get mid
int mid = low + ((high - low) / 2);
// If mid element is >=num search for left half
if (dp.get(mid) >= num) {
ans = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
return ans;
}
static int longestIncreasingSubsequence(ArrayList<Integer> arr, int N) {
ArrayList<Integer> ans = new ArrayList<>();
// Initialize the answer ArrayList with the first element of arr
ans.add(arr.get(0));
for (int i = 1; i < N; i++) {
if (arr.get(i) > ans.get(ans.size() - 1)) {
// If the current number is greater than the last element of the answer ArrayList,
// it means we have found a longer increasing subsequence.
// Hence, we append the current number to the answer ArrayList.
ans.add(arr.get(i));
} else {
// If the current number is not greater than the last element of the answer ArrayList,
// we perform a binary search to find the smallest element in the answer ArrayList
// that is greater than or equal to the current number.
int low = Collections.binarySearch(ans, arr.get(i));
if (low < 0) {
low = -(low + 1);
}
// We update the element at the found position with the current number.
// By doing this, we are maintaining a sorted order in the answer ArrayList.
ans.set(low, arr.get(i));
}
}
// The size of the answer ArrayList represents the length of the longest increasing subsequence.
return ans.size();
}
// Driver code
public static void main(String[] args) {
int N = 5;
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(1, 6, 2, 4, 3));
for (int i = 0; i < N; i++) {
arr.set(i, -arr.get(i));
}
System.out.println(longestIncreasingSubsequence(arr, N));
}
}
C#
using System;
using System.Collections.Generic;
public class LIS
{
// To search for the correct position of num in the array dp
static int Search(List<int> dp, int num)
{
// Initialize low, high, and ans
int low = 0, high = dp.Count - 1;
int ans = -1;
while (low <= high)
{
// Get mid
int mid = low + ((high - low) / 2);
// If mid element is >= num search for left half
if (dp[mid] >= num)
{
ans = mid;
high = mid - 1;
}
else
low = mid + 1;
}
return ans;
}
static int LongestIncreasingSubsequence(List<int> arr, int N)
{
List<int> ans = new List<int>();
// Initialize the answer list with the first element of nums
ans.Add(arr[0]);
for (int i = 1; i < N; i++)
{
if (arr[i] > ans[ans.Count - 1])
{
// If the current number is greater than the last element of the answer
// list, it means we have found a longer increasing subsequence.
// Hence, we append the current number to the answer list.
ans.Add(arr[i]);
}
else
{
// If the current number is not greater than the last element of
// the answer list, we perform a binary search to find the smallest
// element in the answer list that is greater than or equal to the
// current number.
// The BinarySearch method returns the index of the first element that
// is not less than the current number.
int index = ans.BinarySearch(arr[i]);
if (index < 0)
index = ~index; // If not found, BinarySearch returns bitwise complement of the index to insert
// We update the element at the found position with the current number.
// By doing this, we are maintaining a sorted order in the answer list.
ans[index] = arr[i];
}
}
// The length of the answer list represents the length of the
// longest increasing subsequence.
return ans.Count;
}
// Driver code
public static void Main(string[] args)
{
int N = 5;
List<int> arr = new List<int> { 1, 6, 2, 4, 3 };
for (int i = 0; i < N; i++)
arr[i] = -arr[i]; // Negating the array elements to find the longest decreasing subsequence
Console.WriteLine(LongestIncreasingSubsequence(arr, N));
}
}
JavaScript
// Function to search for the correct position of num in the array dp using binary search
function search(dp, num) {
let low = 0, high = dp.length - 1;
let ans = -1;
while (low <= high) {
let mid = low + Math.floor((high - low) / 2);
if (dp[mid] >= num) {
// If mid element is >= num, search for the left half
ans = mid;
high = mid - 1;
} else {
// If mid element is < num, search for the right half
low = mid + 1;
}
}
return ans;
}
// Function to find the length of the longest increasing subsequence
function longestIncreasingSubsequence(arr, N) {
let ans = [];
// Initialize the answer array with the first element of arr
ans.push(arr[0]);
for (let i = 1; i < N; i++) {
if (arr[i] > ans[ans.length - 1]) {
// If the current number is greater than the last element of the answer array,
// it means we have found a longer increasing subsequence.
// Hence, we append the current number to the answer array.
ans.push(arr[i]);
} else {
// If the current number is not greater than the last element of the answer array,
// perform a binary search to find the smallest element in the answer array
// that is greater than or equal to the current number.
let low = ans.findIndex(el => el >= arr[i]);
if (low < 0) {
low = -(low + 1);
}
// Update the element at the found position with the current number.
// By doing this, we are maintaining a sorted order in the answer array.
ans[low] = arr[i];
}
}
// The length of the answer array represents the length of the longest increasing subsequence.
return ans.length;
}
// Driver code
const N = 5;
let arr = [1, 6, 2, 4, 3];
arr = arr.map(num => -num); // Negating elements of the array
console.log(longestIncreasingSubsequence(arr, N));
Python3
import bisect
def longestIncreasingSubsequence(arr):
ans = []
# Initialize the answer list with the
# first element of arr
ans.append(arr[0])
# Traverse through the array starting from
# the second element
for i in range(1, len(arr)):
if arr[i] > ans[-1]:
# If the current number is greater
# than the last element of the answer
# list, it means we have found a
# longer increasing subsequence.
# Hence, we append the current number
# to the answer list.
ans.append(arr[i])
else:
# If the current number is not
# greater than the last element of
# the answer list, we perform
# a binary search to find the smallest
# element in the answer list that
# is greater than or equal to the
# current number.
# The bisect.bisect_left() function
# returns the index where the current
# number should be inserted in order
# to maintain the sorted order.
low = bisect.bisect_left(ans, arr[i])
# We update the element at the
# found position with the current number.
# By doing this, we are maintaining
# a sorted order in the answer list.
ans[low] = arr[i]
# The length of the answer list
# represents the length of the
# longest increasing subsequence.
return len(ans)
# Driver code
if __name__ == "__main__":
arr = [-1, -6, -2, -4, -3]
print(longestIncreasingSubsequence(arr))
Time complexity: O(N * log(N))
Auxiliary Space: O(N)
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem