Find the final sequence of the array after performing given operations | Function that generates the array b [ ] when n is even ; Fill the first half of the final array with reversed sequence ; Fill the second half ; Function that generates the array b [ ] when n is odd ; Fill the first half of the final array with reversed sequence ; Fill the second half ; Function to find the final array b [ ] after n operations of given type ; Create the array b ; If the array size is even ; Print the final array elements ; Driver code
function solveEven ( n , arr , b ) { let left = n - 1 ; for ( let i = 0 ; i < parseInt ( n / 2 , 10 ) ; ++ i ) { b [ i ] = arr [ left ] ; left = left - 2 ; if ( left < 0 ) break ; } let right = 0 ; for ( let i = parseInt ( n / 2 , 10 ) ; i <= n - 1 ; ++ i ) { b [ i ] = arr [ right ] ; right = right + 2 ; if ( right > n - 2 ) break ; } } function solveOdd ( n , arr , b ) { let left = n - 1 ; for ( let i = 0 ; i < parseInt ( n / 2 , 10 ) + 1 ; ++ i ) { b [ i ] = arr [ left ] ; left = left - 2 ; if ( left < 0 ) break ; } let right = 1 ; for ( let i = parseInt ( n / 2 , 10 ) + 1 ; i <= n - 1 ; ++ i ) { b [ i ] = arr [ right ] ; right = right + 2 ; if ( right > n - 2 ) break ; } } function solve ( n , arr ) { let b = new Array ( n ) ; if ( n % 2 == 0 ) solveEven ( n , arr , b ) ; else solveOdd ( n , arr , b ) ; for ( let i = 0 ; i <= n - 1 ; ++ i ) { document . write ( b [ i ] + " " ) ; } } let arr = [ 1 , 2 , 3 , 4 ] ; let n = arr . length ; solve ( n , arr ) ;