- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find number of increasing subsequences of size k in Python
Suppose we have a list of numbers called nums and also another value k, we have to find the number of subsequences of size k that are strictly increasing. If the answer is very large, mod it by 10^9 + 7.
So, if the input is like nums = [2, 3, 4, 1] k = 2, then the output will be 3, as we have the subsequences of size 2: [2, 3], [3, 4], [2, 4].
To solve this, we will follow these steps −
- m := 10^9 + 7
- dp := a list of size same as nums and fill with 1
- iterate the following k times, do
- for j in range size of dp - 1 to 0, decrease by 1, do
- dp[j] := 0
- for i in range 0 to j, do
- if nums[i] < nums[j], then
- dp[j] := dp[j] + dp[i]
- if nums[i] < nums[j], then
- for j in range size of dp - 1 to 0, decrease by 1, do
- return (sum of all elements in dp) mod m
Example (Python)
Let us see the following implementation to get better understanding −
class Solution: def solve(self, nums, k): m = 10 ** 9 + 7 dp = [1] * len(nums) for _ in range(k - 1): for j in range(len(dp) - 1, -1, -1): dp[j] = 0 for i in range(j): if nums[i] < nums[j]: dp[j] += dp[i] return sum(dp) % m ob = Solution() nums = [2, 3, 4, 1] k = 2 print(ob.solve(nums, k))
Input
[2, 3, 4, 1], 2
Output
3
- Related Articles
- Program to find number of distinct subsequences in Python
- Program to find number of consecutive subsequences whose sum is divisible by k in Python
- Program to find number of subsequences with i, j and k number of x, y, z letters in Python
- Program to find number of different subsequences GCDs in Python
- Program to split lists into strictly increasing sublists of size greater than k in Python
- Program to find max values of sublists of size k in Python
- Program to find number of arithmetic subsequences from a list of numbers in Python?
- Program to find lexicographically smallest subsequence of size k in Python
- Program to find number of subsequences that satisfy the given sum condition using Python
- Program to find number of unique subsequences same as target in C++
- Program to find max number of K-sum pairs in Python
- Increasing Subsequences in C++
- Program to find largest average of sublist whose size at least k in Python
- Program to find length of longest increasing subsequence with at least k odd values in Python
- Program to find kth lexicographic sequence from 1 to n of size k Python

Advertisements