In this loop first we will insert one smallest element not entered till that time in final sequence and then enter a highest element ( not entered till that time ) in final sequence so that we have large difference value . This process is repeated till all array has completely entered in sequence . Here , we have loop till n / 2 because we are inserting two elements at a time in loop .
for ( $ i = 0 ; $ i < $ n / 2 ; ++ $ i ) { array_push ( $ finalSequence , $ a [ $ i ] ) ; array_push ( $ finalSequence , $ a [ $ n - $ i - 1 ] ) ; }