Open In App

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

Last Updated : 27 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads