Split the array and add the first part to the end | Function to spilt array and move first part to end ; make a temporary array with double the size ; copy array element in to new array twice ; Driver code
function SplitAndAdd ( A , Length , rotation ) { let tmp = new Array ( Length * 2 ) ; for ( let i = 0 ; i < tmp . length ; i ++ ) { tmp [ i ] = 0 ; } for ( let i = 0 ; i < Length ; i ++ ) { tmp [ i ] = A [ i ] tmp [ i + Length ] = A [ i ] } for ( let i = rotation ; i < rotation + Length ; i ++ ) { A [ i - rotation ] = tmp [ i ] ; } } let arr = [ 12 , 10 , 5 , 6 , 52 , 36 ] ; let n = arr . length ; let position = 2 ; SplitAndAdd ( arr , n , position ) ; for ( let i = 0 ; i < n ; ++ i ) document . write ( arr [ i ] + " " ) ;