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
< ? php function findElement ( $ arr , $ ranges , $ rotations , $ index ) { for ( $ i = $ rotations - 1 ; $ i >= 0 ; $ i -- ) { $ left = $ ranges [ $ i ] [ 0 ] ; $ right = $ ranges [ $ i ] [ 1 ] ; if ( $ left <= $ index && $ right >= $ index ) { if ( $ index == $ left ) $ index = $ right ; else $ index -- ; } } return $ arr [ $ index ] ; } $ arr = array ( 1 , 2 , 3 , 4 , 5 ) ; $ rotations = 2 ; $ ranges = array ( array ( 0 , 2 ) , array ( 0 , 3 ) ) ; $ index = 1 ; echo findElement ( $ arr , $ ranges , $ rotations , $ index ) ; ? >