Last element of an array after repeatedly removing the first element and appending it to the end of the array twice exactly K times | Function to find the last element after performing given operations ; Length of the array ; Increment j until condition is satisfied ; In each pair every value is repeating r number of times ; Print the result according to the value of k ; Driver Code ; Given K ; Given arr [ ] ; Function call
def findLastElement ( N , A ) : NEW_LINE INDENT l = len ( A ) NEW_LINE j = 0 NEW_LINE while ( N > l * ( 2 ** j ) ) : NEW_LINE INDENT N = N - l * 2 ** j NEW_LINE j += 1 NEW_LINE DEDENT k = 1 NEW_LINE r = 2 ** j NEW_LINE for i in range ( 1 , l ) : NEW_LINE INDENT if N > r * i : NEW_LINE INDENT k += 1 NEW_LINE DEDENT DEDENT for i in range ( 0 , len ( A ) ) : NEW_LINE INDENT if ( i + 1 == k ) : NEW_LINE INDENT print ( A [ i ] ) NEW_LINE return NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT K = 7 NEW_LINE A = [ 1 , 2 , 3 ] NEW_LINE findLastElement ( K , A ) NEW_LINE DEDENT