VOOZH about

URL: https://www.geeksforgeeks.org/dsa/cpp-program-for-ways-to-sum-to-n-using-natural-numbers-up-to-k-with-repetitions-allowed/

⇱ C++ Program for Ways to sum to N using Natural Numbers up to K with repetitions allowed - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C++ Program for Ways to sum to N using Natural Numbers up to K with repetitions allowed

Last Updated : 27 Jan, 2022

Given two integers N and K, the task is to find the total number of ways of representing N as the sum of positive integers in the range [1, K], where each integer can be chosen multiple times.

Examples:

Input: N = 8, K = 2
Output: 5
Explanation: All possible ways of representing N as sum of positive integers less than or equal to K are:

  1. {1, 1, 1, 1, 1, 1, 1, 1}, the sum is 8.
  2. {2, 1, 1, 1, 1, 1, 1}, the sum is 8.
  3. {2, 2, 1, 1, 1, 1}, the sum is 8.
  4. 2, 2, 2, 1, 1}, the sum is 8.
  5. {2, 2, 2, 2}}, the sum is 8.

Therefore, the total number of ways is 5.

Input: N = 2, K = 2
Output: 2

Comment