Check if all array elements can be reduced to 0 by repeatedly reducing pairs of consecutive elements by their minimum | Function to check if it is possible to convert the array ; Traverse the array range [ 1 , N - 1 ] ; If arr [ i ] < arr [ i - 1 ] ; Otherwise ; Decrement arr [ i ] by arr [ i - 1 ] ; If arr [ n - 1 ] is not equal to zero ; Otherwise ; Driver Code ; Function Call
def checkPossible ( arr , n ) : NEW_LINE INDENT for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i ] < arr [ i - 1 ] ) : NEW_LINE INDENT print ( " No " ) ; NEW_LINE return ; NEW_LINE DEDENT else : NEW_LINE INDENT arr [ i ] -= arr [ i - 1 ] ; NEW_LINE arr [ i - 1 ] = 0 ; NEW_LINE DEDENT DEDENT if ( arr [ n - 1 ] == 0 ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 3 , 4 , 2 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE checkPossible ( arr , N ) ; NEW_LINE DEDENT