Given an integer N, the task is to print all the possible ways in which N can be written as the sum of two or more positive integers.
Input: N = 4
1 1 1 1
1 1 2
Input: N = 3
1 1 1
Approach: The idea is to use recursion to solve this problem. The idea is to consider every integer from 1 to N such that the sum N can be reduced by this number at each recursive call and if at any recursive call N reduces to zero then we will print the answer stored in the vector. Below are the steps for recursion:
- Get the number N whose sum has to be broken into two or more positive integers.
- Recursively iterate from value 1 to N as index i:
- Base Case: If the value called recursively is 0, then print the current vector as this is the one of the way to broke N into two or more positive integers.
if (n == 0) printVector(arr);
- Recursive Call: If the base case is not met, then Recursively iterate from [i, N – i]. Push the current element j into vector(say arr) and recursively iterate for the next index and after the this recursion ends then pop the element j inserted previously:
for j in range[i, N]: arr.push_back(j); recursive_function(arr, j + 1, N - j); arr.pop_back(j);
Below is the implementation of the above approach:
1 1 1 1 1 1 2 1 3 2 2
Time Complexity: O(2N)
Auxiliary Space: O(N2)
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.
- Ways to write N as sum of two or more positive integers | Set-2
- Number of ways in which N can be represented as the sum of two positive integers
- Find K distinct positive odd integers with sum N
- Find all the possible remainders when N is divided by all positive integers from 1 to N+1
- Count of digits after concatenation of first N positive integers
- Check whether product of integers from a to b is positive , negative or zero
- Represent (2 / N) as the sum of three distinct positive integers of the form (1 / m)
- Find n positive integers that satisfy the given equations
- Count positive integers with 0 as a digit and maximum 'd' digits
- Number of arrays of size N whose elements are positive integers and sum is K
- Check whether a number can be represented as sum of K distinct positive integers
- Maximum number of distinct positive integers that can be used to represent N
- Longest alternating subsequence in terms of positive and negative integers
- Count of ways to write N as a sum of three numbers
- Count ways to express even number ‘n’ as sum of even integers
- Print first k digits of 1/n where n is a positive integer
- Ways to form an array having integers in given range such that total sum is divisible by 2
- Permutation of first N positive integers such that prime numbers are at prime indices
- Permutation of first N positive integers such that prime numbers are at prime indices | Set 2
- Find the number of positive integers less than or equal to N that have an odd number of digits
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.