combinatorics
increasing subsequences
sequence algorithms
mathematical sequences
discrete mathematics

Number of Increasing Subsequences of length k

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

In combinatorics, the problem of determining the number of increasing subsequences of a given length kk from a sequence is a fundamental question with significant implications in algorithm design, bioinformatics, and coding theory. This article examines both foundational concepts and practical techniques for solving this problem efficiently.

Understanding Increasing Subsequences

An increasing subsequence of a sequence is a subsequence where each element is strictly larger than the preceding one. For example, given the sequence 1,3,2,4{1, 3, 2, 4}, possible increasing subsequences include 1,3{1, 3}, 1,2{1, 2}, 1,4{1, 4}, 3,4{3, 4}, 2,4{2, 4}, 1,3,4{1, 3, 4}, 1,2,4{1, 2, 4}, and the single elements 1{1}, 3{3}, 2{2}, 4{4}.

Problem Statement

Given a sequence of nn distinct integers, find the number of increasing subsequences of length exactly kk. This problem can be generalized to include sequences with repeated numbers by using non-strict inequality, but we focus on the strictly increasing case here.

Dynamic Programming Approach

A common way to solve this problem is through dynamic programming. The approach involves building a table where each entry dp[i][j]dp[i][j] represents the number of increasing subsequences of length jj that end at element ii (using 1-based indexing for the sequence).

Initialization

For length 1, every single element forms an increasing subsequence by itself:

dp[i][1]=1for all i1,2,,ndp[i][1] = 1 \quad \text{for all } i \in {1, 2, \ldots, n}

Transition

For every element at position ii and subsequence length jj, count those that can be formed by preceding elements smaller than aia_i:

dp[i][j]=m=1am<aii1dp[m][j1]dp[i][j] = \sum_{\substack{m=1 \\ a_m < a_i}}^{i-1} dp[m][j-1]

In words, to count increasing subsequences of length jj ending at position ii, look at all earlier positions mm where am<aia_m < a_i and sum up the count of increasing subsequences of length j1j-1 ending at those positions.

Final Result

The total number of increasing subsequences of length kk is the sum over all ending positions:

answer=i=1ndp[i][k]\text{answer} = \sum_{i=1}^{n} dp[i][k]

Example

Consider the sequence 2,5,3,7,4{2, 5, 3, 7, 4} and k=3k = 3.

Step 1: Initialize dp[i][1]=1dp[i][1] = 1 for all ii.

iaia_idp[i][1]dp[i][1]
121
251
331
471
541

Step 2: Fill the table for j=2j = 2:

  • dp[2][2]=dp[1][1]=1dp[2][2] = dp[1][1] = 1 (subsequence 2,5{2, 5})
  • dp[3][2]=dp[1][1]=1dp[3][2] = dp[1][1] = 1 (subsequence 2,3{2, 3})
  • dp[4][2]=dp[1][1]+dp[2][1]+dp[3][1]=3dp[4][2] = dp[1][1] + dp[2][1] + dp[3][1] = 3 (subsequences 2,7,5,7,3,7{2,7}, {5,7}, {3,7})
  • dp[5][2]=dp[1][1]+dp[3][1]=2dp[5][2] = dp[1][1] + dp[3][1] = 2 (subsequences 2,4,3,4{2,4}, {3,4})

Step 3: Fill for j=3j = 3:

  • dp[4][3]=dp[2][2]+dp[3][2]=1+1=2dp[4][3] = dp[2][2] + dp[3][2] = 1 + 1 = 2 (subsequences 2,5,7,2,3,7{2,5,7}, {2,3,7})
  • dp[5][3]=dp[3][2]=1dp[5][3] = dp[3][2] = 1 (subsequence 2,3,4{2,3,4})

Answer: dp[4][3]+dp[5][3]=2+1=3dp[4][3] + dp[5][3] = 2 + 1 = 3

Complexity Analysis

The time complexity of this basic algorithm is O(n2k)O(n^2 \cdot k) due to the nested loops iterating over possible subsequences for each element and subsequence length. The space complexity is O(nk)O(n \cdot k) for the DP table.

Optimization with BIT/Fenwick Tree

The inner summation over all preceding elements can be accelerated using a Binary Indexed Tree (BIT). By processing elements in a sorted order and querying prefix sums, each transition step can be done in O(logn)O(\log n) instead of O(n)O(n), bringing the total time complexity down to O(nklogn)O(n \cdot k \cdot \log n). This optimization is important for large sequences.

Summary Table

ConceptDescription
Increasing SubsequenceSubsequence where each element is larger than the previous one
DP Table dp[i][j]dp[i][j]Count of increasing subsequences ending at index ii of length jj
Initializationdp[i][1]=1dp[i][1] = 1 for each ii
Transitiondp[i][j]=m<i,  am<aidp[m][j1]dp[i][j] = \sum_{m < i, \; a_m < a_i} dp[m][j-1]
Basic Time ComplexityO(n2k)O(n^2 \cdot k)
Optimized (with BIT)O(nklogn)O(n \cdot k \cdot \log n)

Conclusion

Computing the number of increasing subsequences of length kk is both a classical and practically relevant problem. The dynamic programming approach provides a clear and correct solution, while data structure optimizations like BIT reduce the time complexity for larger inputs. Understanding this problem also builds intuition for related challenges like the Longest Increasing Subsequence (LIS) and patience sorting.


Related reading
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.