Print all possible ways to write N as sum of two or more positive integers | Java 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
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 ) ; } }