Given two natural number n and m. The task is to find the number of ways in which the numbers that are greater than or equal to m can be added to get the sum n.
Input : n = 3, m = 1 Output : 3 Following are three different ways to get sum n such that each term is greater than or equal to m 1 + 1 + 1, 1 + 2, 3 Input : n = 2, m = 1 Output : 2 Two ways are 1 + 1 and 2
The idea is to use Dynamic Programming by define 2D matrix, say dp. dp[i][j] define the number of ways to get sum i using the numbers greater than or equal to j. So dp[i][j] can be defined as:
If i < j, dp[i][j] = 0, because we cannot achieve smaller sum of i using numbers greater than or equal to j.
If i = j, dp[i][j] = 1, because there is only one way to show sum i using number i which is equal to j.
Else dp[i][j] = dp[i][j+1] + dp[i-j][j], because obtaining a sum i using numbers greater than or equal to j is equal to the sum of obtaining a sum of i using numbers greater than or equal to j+1 and obtaining the sum of i-j using numbers greater than or equal to j.
Below is the implementation of this approach:
- Minimum sum of multiplications of n numbers
- Probability of reaching a point with 2 or 3 steps at a time
- Number of decimal numbers of length k, that are strict monotone
- Counting numbers of n digits that are monotone
- Entringer Number
- Lobb Number
- Value of continuous floor function : F(x) = F(floor(x/2)) + x
- Remove array end element to maximize the sum of product
- Unique paths in a Grid with Obstacles
- Super Ugly Number (Number whose prime factors are in given set)
- Tiling Problem
- Coin Change | DP-7
- Longest Common Subsequence | DP-4
- Ugly Numbers
- Largest Sum Contiguous Subarray
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.