- Related Questions & Answers
- Program to find number of subsequences with i, j and k number of x, y, z letters in Python
- Program to find max values of sublists of size k in Python
- Program to find lexicographically smallest subsequence of size k in Python
- Program to find number of arithmetic subsequences from a list of numbers in Python?
- Program to split lists into strictly increasing sublists of size greater than k in Python
- Increasing Subsequences in C++
- Program to find number of unique subsequences same as target in C++
- Program to find sum of widths of all subsequences of list of numbers in Python
- Program to find largest average of sublist whose size at least k in Python
- Program to find kth lexicographic sequence from 1 to n of size k Python
- Program to find length of longest increasing subsequence with at least k odd values in Python
- Program to find length of longest increasing subsequence in Python
- Product of all Subsequences of size K except the minimum and maximum Elements in C++
- Program to find minimum number of operations required to make lists strictly Increasing in python
- Program to find length of contiguous strictly increasing sublist in Python

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

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))

[2, 3, 4, 1], 2

3

Advertisements