Find element at given index after a number of rotations | Function to compute the element at given index ; Range [ left ... right ] ; Rotation will not have any effect ; Returning new element ; Driver Code ; No . of rotations ; Ranges according to 0 - based indexing
let findElement = ( arr , ranges , rotations , index ) => { for ( let i = rotations - 1 ; i >= 0 ; i -- ) { let left = ranges [ i ] [ 0 ] ; let right = ranges [ i ] [ 1 ] ; if ( left <= index && right >= index ) { if ( index == left ) index = right ; else index -- ; } } return arr [ index ] ; } let arr = [ 1 , 2 , 3 , 4 , 5 ] ; let rotations = 2 ; let ranges = [ [ 0 , 2 ] , [ 0 , 3 ] ] ; let index = 1 ; document . write ( findElement ( arr , ranges , rotations , index ) ) ;