Print all possible ways to write N as sum of two or more positive integers
Last Updated :
02 Feb, 2022
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:
- 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 one of the ways 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 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:
C++
#include <bits/stdc++.h>
using namespace std;
void printVector(vector< int >& arr)
{
if (arr.size() != 1) {
for ( int i = 0; i < arr.size(); i++) {
cout << arr[i] << " " ;
}
cout << endl;
}
}
void findWays(vector< int >& arr, int i, int n)
{
if (n == 0)
printVector(arr);
for ( int j = i; j <= n; j++) {
arr.push_back(j);
findWays(arr, j, n - j);
arr.pop_back();
}
}
int main()
{
int n = 4;
vector< int > arr;
findWays(arr, 1, n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void printVector(ArrayList<Integer> arr)
{
if (arr.size() != 1 )
{
for ( int i = 0 ; i < arr.size(); i++)
{
System.out.print(arr.get(i) + " " );
}
System.out.println();
}
}
static void findWays(ArrayList<Integer> arr,
int i, int n)
{
if (n == 0 )
printVector(arr);
for ( int j = i; j <= n; j++)
{
arr.add(j);
findWays(arr, j, n - j);
arr.remove(arr.size() - 1 );
}
}
public static void main(String[] args)
{
int n = 4 ;
ArrayList<Integer> arr = new ArrayList<Integer>();
findWays(arr, 1 , n);
}
}
|
Python3
def printVector(arr):
if ( len (arr) ! = 1 ):
for i in range ( len (arr)):
print (arr[i], end = " " )
print ()
def findWays(arr, i, n):
if (n = = 0 ):
printVector(arr)
for j in range (i, n + 1 ):
arr.append(j)
findWays(arr, j, n - j)
del arr[ - 1 ]
if __name__ = = '__main__' :
n = 4
arr = []
findWays(arr, 1 , n)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void printList(List< int > arr)
{
if (arr.Count != 1)
{
for ( int i = 0; i < arr.Count; i++)
{
Console.Write(arr[i] + " " );
}
Console.WriteLine();
}
}
static void findWays(List< int > arr,
int i, int n)
{
if (n == 0)
printList(arr);
for ( int j = i; j <= n; j++)
{
arr.Add(j);
findWays(arr, j, n - j);
arr.RemoveAt(arr.Count - 1);
}
}
public static void Main(String[] args)
{
int n = 4;
List< int > arr = new List< int >();
findWays(arr, 1, n);
}
}
|
Javascript
<script>
function printVector(arr)
{
if (arr.length != 1) {
for ( var i = 0; i < arr.length; i++) {
document.write( arr[i] + " " );
}
document.write( "<br>" );
}
}
function findWays(arr, i, n)
{
if (n == 0)
printVector(arr);
for ( var j = i; j <= n; j++) {
arr.push(j);
findWays(arr, j, n - j);
arr.pop();
}
}
var n = 4;
var arr = [];
findWays(arr, 1, n);
</script>
|
Output:
1 1 1 1
1 1 2
1 3
2 2
Time Complexity: O(2N)
Auxiliary Space: O(N2)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...