Represent K as sum of N | Java program for the above problem ; Array to store the N - Bonacci series ; Function to express K as sum of several N_bonacci numbers ; Driver code
import java . util . * ; class GFG { public static long [ ] N_bonacci = new long [ 100 ] ; @ SuppressWarnings ( " unchecked " ) public static void N_bonacci_nums ( int n , int k ) { N_bonacci [ 0 ] = 1 ; for ( int i = 1 ; i <= 50 ; ++ i ) { for ( int j = i - 1 ; j >= i - k && j >= 0 ; -- j ) N_bonacci [ i ] += N_bonacci [ j ] ; } Vector ans = new Vector ( ) ; for ( int i = 50 ; i >= 0 ; -- i ) if ( n - N_bonacci [ i ] >= 0 ) { ans . add ( N_bonacci [ i ] ) ; n -= N_bonacci [ i ] ; } if ( ans . size ( ) == 1 ) ans . add ( 0 ) ; System . out . println ( ans . size ( ) ) ; for ( int i = 0 ; i < ans . size ( ) ; ++ i ) System . out . print ( ans . get ( i ) + " , ▁ " ) ; } public static void main ( String args [ ] ) { int n = 21 , k = 5 ; N_bonacci_nums ( n , k ) ; } }