VOOZH about

URL: https://www.geeksforgeeks.org/dsa/different-ways-sum-n-using-numbers-greater-equal-m/

⇱ Different ways to sum n using numbers greater than or equal to m - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Different ways to sum n using numbers greater than or equal to m

Last Updated : 2 Dec, 2024

Given two natural numbers 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.

Examples:

Input: n = 3, m = 1
Output: 3
Explanation: Three different ways to get sum n such that each term is greater than or equal to m are 1 + 1 + 1, 1 + 2 and 3

Input: n = 2, m = 1
Output: 2
Explanation: Two different ways to get sum n such that each term is greater than or equal to m are 1 + 1 and 2

Using Recursion - O(2^n) Time and O(n) Space

The idea is to find the number of ways to reach n by trying each value of m from m to n. Starting from the target sum n, for each m, we can either include it or exclude it. If we include it, we subtract its value from sum and recursively try to make the remaining amount with the same value. If we exclude it, we move to (m+1).

Mathematically the recurrence relation will look like the following:

  • count(m, n) = count(m, n-m) + count(m+1, n)

Base cases:

  • count(m, n) = 0, if n < 0 or m > n.
  • count(m, n) = 1, if n == 0.

Output
3

Using Top-Down DP (Memoization) - O(n^2) Time and O(n^2) Space

If we notice carefully, we can observe that the above recursive solution holds the following two properties of Dynamic Programming:

1. Optimal Substructure: Number of ways to make sum n at value m, i.e., count(m, n), depends on the optimal solutions of the subproblems count(m, n-m) and count(m+1, n). By combining these optimal substructures, we can efficiently calculate the number of ways to make target sum n at value m.

2. Overlapping Subproblems: While applying a recursive approach in this problem, we notice that certain subproblems are computed multiple times.

  • There are only are two parameters: m and n that changes in the recursive solution. The value of m will be in range [m, n] So we create a 2D matrix of size (n+1)*(n+1) for memoization.
  • We initialize this matrix as -1 to indicate nothing is computed initially.
  • Now we modify our recursive solution to first check if the value is -1, then only make recursive calls. This way, we avoid re-computations of the same subproblems.

Output
3

Using Bottom-Up DP (Tabulation) - O(n^2) Time and O(n^2) Space

The idea is to fill the DP table based on previous values. For each value m, we either include it or exclude it to compute the number of ways needed for each sum n. The table is filled in an iterative manner from i = n to i = m and for each sum from 1 to n.

The dynamic programming relation is as follows: 

  • if (sum-i) is greater than equal to 0, then dp[i][sum] = dp[i][sum-i] + dp[i+1][sum]
  • else dp[i][sum] = dp[i+1][sum].

Output
3

Using Space Optimized DP - O(n^2) Time and O(n) Space

In previous approach of dynamic programming we have derive the relation between states as given below:

  • if (sum-i) is greater than 0, then dp[i][sum] = dp[i][sum-i] + dp[i+1][sum]
  • else dp[i][sum] = dp[i+1][sum].

If we observe that for calculating current dp[i][sum] state we only need previous row dp[i-1][sum] or current row dp[i][sum-i]. There is no need to store all the previous states just one previous state is used to compute result.


Output
3
Comment
Article Tags: