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.
Examples:
Input:_ N = 4_
Output:
1 1 1 1
1 1 2
1 3
2 2
Input:_ N = 3_
Output:
1 1 1
1 2
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:
if (n == 0)
printVector(arr);
for j in range[i, N]:
arr.push_back(j);
recursive_function(arr, j + 1, N - j);
arr.pop_back(j);
#backtracking #greedy #arrays #algorithms