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
< ? php function rearrange ( & $ arr , $ n ) { if ( $ arr == NULL $ n % 2 == 1 ) return ; $ currIdx = intval ( ( $ n - 1 ) / 2 ) ; while ( $ currIdx > 0 ) { $ count = $ currIdx ; $ swapIdx = $ currIdx ; while ( $ count -- > 0 ) { $ temp = $ arr [ $ swapIdx + 1 ] ; $ arr [ $ swapIdx + 1 ] = $ arr [ $ swapIdx ] ; $ arr [ $ swapIdx ] = $ temp ; $ swapIdx ++ ; } $ currIdx -- ; } } $ arr = array ( 1 , 3 , 5 , 2 , 4 , 6 ) ; $ n = count ( $ arr ) ; rearrange ( $ arr , $ n ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo ( $ arr [ $ i ] . " ▁ " ) ; ? >