text: Print all possible ways to write N as sum of two or more positive integers | C ++ program for the above approach ; Function to print the values stored in vector arr ; Traverse the vector arr ; Recursive function to print different ways in which N can be written as a sum of at 2 or more positive integers ; If n is zero then print this ways of breaking numbers ; Start from previous element in the representation till n ; Include current element from representation ; Call function again with reduced sum ; Backtrack to remove current element from representation ; Driver Code ; Given sum N ; To store the representation of breaking N ; Function Call
code: #include <bits/stdc++.h> NEW_LINE 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 ; }