Split a number as sum of K numbers which are not divisible by K | Function to split into K parts and print them ; Print 1 K - 1 times ; Print N - K + 1 ; Print 1 K - 2 times ; Print 2 and N - K ; Driver code
function printKParts ( N , K ) { if ( N % K == 0 ) { for ( var i = 1 ; i < K ; i ++ ) document . write ( " " ) ; document . write ( ( N - ( K - 1 ) ) + " " ) ; } else { if ( K == 2 ) { document . write ( " " + " " ) ; return ; } for ( var i = 1 ; i < K - 1 ; i ++ ) document . write ( 1 + " " ) ; document . write ( 2 + " " + ( N - K ) + " " ) ; } } var N = 18 , K = 5 ; printKParts ( N , K ) ;