Friday, January 10, 2025
Google search engine
HomeData Modelling & AIC++ Program for Ways to sum to N using Natural Numbers up...

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

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

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments