Block swap algorithm for array rotation |  ; Return If number of elements to be rotated is zero or equal to array size ; If number of elements to be rotated is exactly half of array size ; If A is shorter ; If B is shorter ; function to print an array ; This function swaps d elements starting at index fi with d elements starting at index si ; Driver Code
let leftRotate = ( arr , d , n ) => { if ( d == 0 d == n ) return ; if ( n - d == d ) { arr = swap ( arr , 0 , n - d , d ) ; return ; } if ( d < n - d ) { arr = swap ( arr , 0 , n - d , d ) ; leftRotate ( arr , d , n - d ) ; } else { arr = swap ( arr , 0 , d , n - d ) ; leftRotate ( arr + n - d , 2 * d - n , d ) ; } } let printArray = ( arr , size ) => { ans = ' ' for ( let i = 0 ; i < size ; i ++ ) ans += arr [ i ] + " " ; document . write ( ans ) } let swap = ( arr , fi , si , d ) => { for ( let i = 0 ; i < d ; i ++ ) { let temp = arr [ fi + i ] ; arr [ fi + i ] = arr [ si + i ] ; arr [ si + i ] = temp ; } return arr } arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] ; leftRotate ( arr , 2 , 7 ) ; printArray ( arr , 7 ) ;