Find the Mth element of the Array after K left rotations | Function to return Mth element of array after k left rotations ; The array comes to original state after N rotations ; Mth element after k left rotations is ( K + M - 1 ) % N th element of the original array ; Return the result ; Array initialization ; Size of the array ; Given K rotation and Mth element to be found after K rotation ; Function call
function getFirstElement ( a , N , K , M ) { K %= N ; var index = ( K + M - 1 ) % N ; var result = a [ index ] ; return result ; } var a = [ 3 , 4 , 5 , 23 ] ; var N = a . length ; var K = 2 , M = 1 ; document . write ( getFirstElement ( a , N , K , M ) ) ;