Remaining array element after repeated removal of last element and subtraction of each element from next adjacent element | Function to find the last remaining array element after performing the given operations repeatedly ; Stores the resultant sum ; Traverse the array ; Increment sum by arr [ i ] * coefficient of i - th term in ( x - y ) ^ ( N - 1 ) ; Update multiplier ; Return the resultant sum ; Driver Code
def lastElement ( arr , n ) : NEW_LINE INDENT sum = 0 NEW_LINE if n % 2 == 0 : NEW_LINE INDENT multiplier = - 1 NEW_LINE DEDENT else : NEW_LINE INDENT multiplier = 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT sum += arr [ i ] * multiplier NEW_LINE multiplier = multiplier * ( n - 1 - i ) / ( i + 1 ) * ( - 1 ) NEW_LINE DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 4 , 2 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE print ( int ( lastElement ( arr , N ) ) ) NEW_LINE DEDENT