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.

**Examples:**

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:

## C++

`// CPP Program to find number of ways to ` `// which numbers that are greater than ` `// given number can be added to get sum. ` `#include <bits/stdc++.h> ` `#define MAX 100 ` `using` `namespace` `std; ` ` ` `// Return number of ways to which numbers ` `// that are greater than given number can ` `// be added to get sum. ` `int` `numberofways(` `int` `n, ` `int` `m) ` `{ ` ` ` `int` `dp[n+2][n+2]; ` ` ` `memset` `(dp, 0, ` `sizeof` `(dp)); ` ` ` ` ` `dp[0][n + 1] = 1; ` ` ` ` ` `// Filling the table. k is for numbers ` ` ` `// greater than or equal that are allowed. ` ` ` `for` `(` `int` `k = n; k >= m; k--) { ` ` ` ` ` `// i is for sum ` ` ` `for` `(` `int` `i = 0; i <= n; i++) { ` ` ` ` ` `// initializing dp[i][k] to number ` ` ` `// ways to get sum using numbers ` ` ` `// greater than or equal k+1 ` ` ` `dp[i][k] = dp[i][k + 1]; ` ` ` ` ` `// if i > k ` ` ` `if` `(i - k >= 0) ` ` ` `dp[i][k] = (dp[i][k] + dp[i - k][k]); ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `dp[n][m]; ` `} ` ` ` `// Driver Program ` `int` `main() ` `{ ` ` ` `int` `n = 3, m = 1; ` ` ` `cout << numberofways(n, m) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Program to find number of ways to ` `// which numbers that are greater than ` `// given number can be added to get sum. ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Return number of ways to which numbers ` ` ` `// that are greater than given number can ` ` ` `// be added to get sum. ` ` ` `static` `int` `numberofways(` `int` `n, ` `int` `m) ` ` ` `{ ` ` ` `int` `dp[][]=` `new` `int` `[n+` `2` `][n+` `2` `]; ` ` ` ` ` `dp[` `0` `][n + ` `1` `] = ` `1` `; ` ` ` ` ` `// Filling the table. k is for numbers ` ` ` `// greater than or equal that are allowed. ` ` ` `for` `(` `int` `k = n; k >= m; k--) { ` ` ` ` ` `// i is for sum ` ` ` `for` `(` `int` `i = ` `0` `; i <= n; i++) { ` ` ` ` ` `// initializing dp[i][k] to number ` ` ` `// ways to get sum using numbers ` ` ` `// greater than or equal k+1 ` ` ` `dp[i][k] = dp[i][k + ` `1` `]; ` ` ` ` ` `// if i > k ` ` ` `if` `(i - k >= ` `0` `) ` ` ` `dp[i][k] = (dp[i][k] + dp[i - k][k]); ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `dp[n][m]; ` ` ` `} ` ` ` ` ` `// Driver Program ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `n = ` `3` `, m = ` `1` `; ` ` ` `System.out.println(numberofways(n, m)); ` ` ` `} ` `} ` ` ` `/*This code is contributed by Nikita tiwari.*/` |

*chevron_right*

*filter_none*

## Python3

`# Python3 Program to find number of ways to ` `# which numbers that are greater than ` `# given number can be added to get sum. ` `MAX` `=` `100` `import` `numpy as np ` ` ` `# Return number of ways to which numbers ` `# that are greater than given number can ` `# be added to get sum. ` ` ` `def` `numberofways(n, m) : ` ` ` ` ` `dp ` `=` `np.zeros((n ` `+` `2` `, n ` `+` `2` `)) ` ` ` ` ` `dp[` `0` `][n ` `+` `1` `] ` `=` `1` ` ` ` ` `# Filling the table. k is for numbers ` ` ` `# greater than or equal that are allowed. ` ` ` `for` `k ` `in` `range` `(n, m ` `-` `1` `, ` `-` `1` `) : ` ` ` ` ` `# i is for sum ` ` ` `for` `i ` `in` `range` `(n ` `+` `1` `) : ` ` ` ` ` `# initializing dp[i][k] to number ` ` ` `# ways to get sum using numbers ` ` ` `# greater than or equal k+1 ` ` ` `dp[i][k] ` `=` `dp[i][k ` `+` `1` `] ` ` ` ` ` `# if i > k ` ` ` `if` `(i ` `-` `k >` `=` `0` `) : ` ` ` `dp[i][k] ` `=` `(dp[i][k] ` `+` `dp[i ` `-` `k][k]) ` ` ` ` ` `return` `dp[n][m] ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n, m ` `=` `3` `, ` `1` ` ` `print` `(numberofways(n, m)) ` ` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find number of ways to ` `// which numbers that are greater than ` `// given number can be added to get sum. ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Return number of ways to which numbers ` ` ` `// that are greater than given number can ` ` ` `// be added to get sum. ` ` ` `static` `int` `numberofways(` `int` `n, ` `int` `m) ` ` ` `{ ` ` ` `int` `[, ] dp = ` `new` `int` `[n + 2, n + 2]; ` ` ` ` ` `dp[0, n + 1] = 1; ` ` ` ` ` `// Filling the table. k is for numbers ` ` ` `// greater than or equal that are allowed. ` ` ` `for` `(` `int` `k = n; k >= m; k--) { ` ` ` ` ` `// i is for sum ` ` ` `for` `(` `int` `i = 0; i <= n; i++) { ` ` ` ` ` `// initializing dp[i][k] to number ` ` ` `// ways to get sum using numbers ` ` ` `// greater than or equal k+1 ` ` ` `dp[i, k] = dp[i, k + 1]; ` ` ` ` ` `// if i > k ` ` ` `if` `(i - k >= 0) ` ` ` `dp[i, k] = (dp[i, k] + dp[i - k, k]); ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `dp[n, m]; ` ` ` `} ` ` ` ` ` `// Driver Program ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 3, m = 1; ` ` ` `Console.WriteLine(numberofways(n, m)); ` ` ` `} ` `} ` ` ` `/*This code is contributed by vt_m.*/` |

*chevron_right*

*filter_none*

## PHP

`<?php ` ` ` `// PHP Program to find number of ways to ` `// which numbers that are greater than ` `// given number can be added to get sum. ` ` ` `$MAX` `= 100; ` ` ` `// Return number of ways to which numbers ` `// that are greater than given number can ` `// be added to get sum. ` `function` `numberofways(` `$n` `, ` `$m` `) ` `{ ` ` ` `global` `$MAX` `; ` ` ` `$dp` `= ` `array_fill` `(0, ` `$n` `+ 2, ` `array_fill` `(0, ` `$n` `+2, NULL)); ` ` ` ` ` `$dp` `[0][` `$n` `+ 1] = 1; ` ` ` ` ` `// Filling the table. k is for numbers ` ` ` `// greater than or equal that are allowed. ` ` ` `for` `(` `$k` `= ` `$n` `; ` `$k` `>= ` `$m` `; ` `$k` `--) ` ` ` `{ ` ` ` ` ` `// i is for sum ` ` ` `for` `(` `$i` `= 0; ` `$i` `<= ` `$n` `; ` `$i` `++) ` ` ` `{ ` ` ` ` ` `// initializing dp[i][k] to number ` ` ` `// ways to get sum using numbers ` ` ` `// greater than or equal k+1 ` ` ` `$dp` `[` `$i` `][` `$k` `] = ` `$dp` `[` `$i` `][` `$k` `+ 1]; ` ` ` ` ` `// if i > k ` ` ` `if` `(` `$i` `- ` `$k` `>= 0) ` ` ` `$dp` `[` `$i` `][` `$k` `] = (` `$dp` `[` `$i` `][` `$k` `] + ` `$dp` `[` `$i` `- ` `$k` `][` `$k` `]); ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `$dp` `[` `$n` `][` `$m` `]; ` `} ` ` ` ` ` `// Driver Program ` ` ` `$n` `= 3; ` ` ` `$m` `= 1; ` ` ` `echo` `numberofways(` `$n` `, ` `$m` `) ; ` ` ` `return` `0; ` ` ` ` ` `// This code is contributed by ChitraNayal ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

3

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Maximum sum subarray having sum less than or equal to given sum using Set
- Smallest subarray from a given Array with sum greater than or equal to K | Set 2
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Calculate Stirling numbers which represents the number of ways to arrange r objects around n different circles
- Number of ways to select equal sized subarrays from two arrays having atleast K equal pairs of elements
- Count numbers (smaller than or equal to N) with given digit sum
- Count of different ways to express N as the sum of 1, 3 and 4
- Ways to fill N positions using M colors such that there are exactly K pairs of adjacent different colors
- Count all square sub-matrix with sum greater than the given number S
- Number of ways to divide an array into K equal sum sub-arrays
- Ways to arrange Balls such that adjacent balls are of different types
- 3 Different ways to print Fibonacci series in Java
- Find maximum sum array of length less than or equal to m
- Count of subsequences in an array with sum less than or equal to X
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Count of subarrays whose maximum element is greater than k
- Number of ways to represent a number as sum of k fibonacci numbers
- Number of ways to split N as sum of K numbers from the given range

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.