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 all the possible remainders when N is divided by all positive integers from 1 to N+1
- Print all integers that are sum of powers of two given numbers
- Print all possible ways to convert one string into another string | Edit-Distance
- 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
- Represent (2 / N) as the sum of three distinct positive integers of the form (1 / m)
- Find K distinct positive odd integers with sum N
- Count of ways to write N as a sum of three numbers
- Minimum Possible sum of digits in a positive multiple of N
- Flip minimum signs of array elements to get minimum sum of positive elements possible
- Print N-bit binary numbers having more 1’s than 0’s in all prefixes
- Count 'd' digit positive integers with 0 as a digit
- Count positive integers with 0 as a digit and maximum 'd' digits
- Find n positive integers that satisfy the given equations
- Check whether product of integers from a to b is positive , negative or zero
- Maximum number of distinct positive integers that can be used to represent N
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Longest alternating subsequence in terms of positive and negative integers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.