Shuffle 2 n integers as a1 | Function to rearrange the array ; If size is null or odd return because it is not possible to rearrange ; Start from the middle index ; Each time we will set two elements from the start to the valid position by swapping ; Driver code
function rearrange ( arr , n ) { if ( arr == null n % 2 == 1 ) return ; let currIdx = Math . floor ( ( n - 1 ) / 2 ) ; while ( currIdx > 0 ) { let count = currIdx , swapIdx = currIdx ; while ( count -- > 0 ) { let temp = arr [ swapIdx + 1 ] ; arr [ swapIdx + 1 ] = arr [ swapIdx ] ; arr [ swapIdx ] = temp ; swapIdx ++ ; } currIdx -- ; } } let arr = [ 1 , 3 , 5 , 2 , 4 , 6 ] ; let n = arr . length ; rearrange ( arr , n ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( " " + arr [ i ] ) ;