Represent N as sum of K even or K odd numbers with repetitions allowed | Function to find the array with all the even / odd elements ; First let 's check  kth is odd or even ; if last element is also an odd number then we can choose odd elements for our answer ; Add 1 in the array ( k - 1 ) times ; Add last odd element ; If array of even elements would be the answer then k - 1 elements would be 2 ; if last element is also an even number then we can choose even elements for our answer ; Add 2 in the array ( k - 1 ) times ; Add last even element ; Printing the array ; Driver Code
def getArrayOfSizeK ( n , k ) : NEW_LINE INDENT ans = [ ] NEW_LINE odd = n - ( ( k - 1 ) * 1 ) NEW_LINE if ( odd > 0 and odd % 2 != 0 ) : NEW_LINE INDENT for i in range ( k - 1 ) : NEW_LINE INDENT ans . append ( 1 ) NEW_LINE DEDENT ans . append ( odd ) NEW_LINE DEDENT even = n - ( ( k - 1 ) * 2 ) NEW_LINE if ( even > 0 and even % 2 == 0 and len ( ans ) == 0 ) : NEW_LINE INDENT for i in range ( k - 1 ) : NEW_LINE INDENT ans . append ( 2 ) NEW_LINE DEDENT ans . append ( even ) NEW_LINE DEDENT if ( len ( ans ) > 0 ) : NEW_LINE INDENT for i in range ( k ) : NEW_LINE INDENT print ( ans [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n , k = 10 , 3 NEW_LINE getArrayOfSizeK ( n , k ) NEW_LINE DEDENT