Split a number as sum of K numbers which are not divisible by K | Java program to 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
class GFG { static void printKParts ( int N , int K ) { if ( N % K == 0 ) { for ( int i = 1 ; i < K ; i ++ ) System . out . print ( "1 , ▁ " ) ; System . out . print ( N - ( K - 1 ) + "NEW_LINE"); } else { if ( K == 2 ) { System . out . print ( " Not ▁ Possible " + "NEW_LINE"); return ; } for ( int i = 1 ; i < K - 1 ; i ++ ) System . out . print ( 1 + " , ▁ " ) ; System . out . print ( 2 + " , ▁ " + ( N - K ) + "NEW_LINE"); } } public static void main ( String [ ] args ) { int N = 18 , K = 5 ; printKParts ( N , K ) ; } }