Subtraction in the Array | Function to perform the given operation on arr [ ] ; Skip elements which are 0 ; Pick smallest non - zero element ; If all the elements of arr [ ] are 0 ; Driver code
def operations ( arr , n , k ) : NEW_LINE INDENT arr . sort ( ) ; NEW_LINE i = 0 ; sum = 0 ; NEW_LINE while ( k > 0 ) : NEW_LINE INDENT while ( i < n and arr [ i ] - sum == 0 ) : NEW_LINE INDENT i += 1 ; NEW_LINE DEDENT if ( i < n and arr [ i ] - sum > 0 ) : NEW_LINE INDENT print ( arr [ i ] - sum , end = " ▁ " ) ; NEW_LINE sum = arr [ i ] ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( 0 ) ; NEW_LINE DEDENT k -= 1 ; NEW_LINE DEDENT DEDENT k = 5 ; NEW_LINE arr = [ 3 , 6 , 4 , 2 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE operations ( arr , n , k ) ; NEW_LINE