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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to print the values stored
// in vector arr
void printVector(vector<int>& arr)
{
if (arr.size() != 1) {
// Traverse the vector arr
for (int i = 0; i < arr.size(); i++) {
cout << arr[i] << " ";
}
cout << endl;
}
}
// Recursive function to print different
// ways in which N can be written as
// a sum of at 2 or more positive integers
void findWays(vector<int>& arr, int i, int n)
{
// If n is zero then print this
// ways of breaking numbers
if (n == 0)
printVector(arr);
// Start from previous element
// in the representation till n
for (int j = i; j <= n; j++) {
// Include current element
// from representation
arr.push_back(j);
// Call function again
// with reduced sum
findWays(arr, j, n - j);
// Backtrack to remove current
// element from representation
arr.pop_back();
}
}
// Driver Code
int main()
{
// Given sum N
int n = 4;
// To store the representation
// of breaking N
vector<int> arr;
// Function Call
findWays(arr, 1, n);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to print the values stored
// in vector arr
static void printVector(ArrayList<Integer> arr)
{
if (arr.size() != 1)
{
// Traverse the vector arr
for(int i = 0; i < arr.size(); i++)
{
System.out.print(arr.get(i) + " ");
}
System.out.println();
}
}
// Recursive function to print different
// ways in which N can be written as
// a sum of at 2 or more positive integers
static void findWays(ArrayList<Integer> arr,
int i, int n)
{
// If n is zero then print this
// ways of breaking numbers
if (n == 0)
printVector(arr);
// Start from previous element
// in the representation till n
for(int j = i; j <= n; j++)
{
// Include current element
// from representation
arr.add(j);
// Call function again
// with reduced sum
findWays(arr, j, n - j);
// Backtrack to remove current
// element from representation
arr.remove(arr.size() - 1);
}
}
// Driver code
public static void main(String[] args)
{
// Given sum N
int n = 4;
// To store the representation
// of breaking N
ArrayList<Integer> arr = new ArrayList<Integer>();
// Function call
findWays(arr, 1, n);
}
}
// This code is contributed by offbeat
Python3
# Python3 program for the above approach
# Function to print the values stored
# in vector arr
def printVector(arr):
if (len(arr) != 1):
# Traverse the vector arr
for i in range(len(arr)):
print(arr[i], end = " ")
print()
# Recursive function to print different
# ways in which N can be written as
# a sum of at 2 or more positive integers
def findWays(arr, i, n):
# If n is zero then print this
# ways of breaking numbers
if (n == 0):
printVector(arr)
# Start from previous element
# in the representation till n
for j in range(i, n + 1):
# Include current element
# from representation
arr.append(j)
# Call function again
# with reduced sum
findWays(arr, j, n - j)
# Backtrack to remove current
# element from representation
del arr[-1]
# Driver Code
if __name__ == '__main__':
# Given sum N
n = 4
# To store the representation
# of breaking N
arr = []
# Function Call
findWays(arr, 1, n)
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to print the values stored
// in vector arr
static void printList(List<int> arr)
{
if (arr.Count != 1)
{
// Traverse the vector arr
for(int i = 0; i < arr.Count; i++)
{
Console.Write(arr[i] + " ");
}
Console.WriteLine();
}
}
// Recursive function to print different
// ways in which N can be written as
// a sum of at 2 or more positive integers
static void findWays(List<int> arr,
int i, int n)
{
// If n is zero then print this
// ways of breaking numbers
if (n == 0)
printList(arr);
// Start from previous element
// in the representation till n
for(int j = i; j <= n; j++)
{
// Include current element
// from representation
arr.Add(j);
// Call function again
// with reduced sum
findWays(arr, j, n - j);
// Backtrack to remove current
// element from representation
arr.RemoveAt(arr.Count - 1);
}
}
// Driver code
public static void Main(String[] args)
{
// Given sum N
int n = 4;
// To store the representation
// of breaking N
List<int> arr = new List<int>();
// Function call
findWays(arr, 1, n);
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// Javascript program for the above approach
// Function to print the values stored
// in vector arr
function printVector(arr)
{
if (arr.length != 1) {
// Traverse the vector arr
for (var i = 0; i < arr.length; i++) {
document.write( arr[i] + " ");
}
document.write("<br>");
}
}
// Recursive function to print different
// ways in which N can be written as
// a sum of at 2 or more positive integers
function findWays(arr, i, n)
{
// If n is zero then print this
// ways of breaking numbers
if (n == 0)
printVector(arr);
// Start from previous element
// in the representation till n
for (var j = i; j <= n; j++) {
// Include current element
// from representation
arr.push(j);
// Call function again
// with reduced sum
findWays(arr, j, n - j);
// Backtrack to remove current
// element from representation
arr.pop();
}
}
// Driver Code
// Given sum N
var n = 4;
// To store the representation
// of breaking N
var arr = [];
// Function Call
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)
Similar Reads
Ways to write N as sum of two or more positive integers | Set-2 Given a number N, the task is to find the number of ways N can be partitioned, i.e. the number of ways that N can be expressed as a sum of positive integers.Note: N should also be considered itself a way to express it as a sum of positive integers. Examples: Input: N = 5 Output: 7 5 can be partition
5 min read
Ways to write n as sum of two or more positive integers For a given number n > 0, find the number of different ways in which n can be written as a sum of two or more positive integers. Examples: Input : n = 5 Output : 6 Explanation : All possible six ways are : 4 + 1 3 + 2 3 + 1 + 1 2 + 2 + 1 2 + 1 + 1 + 1 1 + 1 + 1 + 1 + 1 Input : 4 Output : 4 Explan
6 min read
Number of ways in which N can be represented as the sum of two positive integers Given a number N, the task is to find the number of unique ways in which N can be represented as a sum of two positive integers.Examples: Input: N = 7 Output: 3 (1 + 6), (2 + 5) and (3 + 4).Input: N = 200 Output: 100 Approach: The number of ways in which the number can be expressed as the sum of two
3 min read
Number of ways to write N as a sum of K non-negative integers Given two positive integers N and K, the task is to count the number of ways to write N as a sum of K non-negative integers. Examples: Input: N = 2, K = 3 Output: 6 Explanation: The total ways in which 2 can be split into K non-negative integers are: 1. (0, 0, 2) 2. (0, 2, 0) 3. (2, 0, 0) 4. (0, 1,
15+ min read
Print any pair of integers with sum of GCD and LCM equals to N Given an integer N, the task is to print any pair of integers that have the sum of GCD and LCM equal to N.Examples: Input: N = 14 Output: 1, 13 Explanation: For the given pair we have GCD(1, 13) = 1 and LCM (1, 13) = 13. Sum of GCD and LCM = 1 + 13 = 14. Input: 2 Output: 1 1 Explanation: For the giv
3 min read