Mth element after K Right Rotations of an Array | Function to return Mth element of array after k right rotations ; The array comes to original state after N rotations ; If K is greater or equal to M ; Mth element after k right rotations is ( N - K ) + ( M - 1 ) th element of the array ; Otherwise ; ( M - K - 1 ) th element of the array ; Return the result ; Driver Code
function getFirstElement ( a , N , K , M ) { K %= N ; let index ; if ( K >= M ) index = ( N - K ) + ( M - 1 ) ; else index = ( M - K - 1 ) ; let result = a [ index ] ; return result ; } let a = [ 1 , 2 , 3 , 4 , 5 ] ; let N = 5 ; let K = 3 , M = 2 ; document . write ( getFirstElement ( a , N , K , M ) ) ;