Array obtained by repeatedly reversing array after every insertion from given array | Function to generate the array by inserting array elements one by one followed by reversing the array ; Doubly ended Queue ; Iterate over the array ; Push array elements alternately to the front and back ; If size of list is odd ; Reverse the list ; Print the elements of the array ; Driver Code
function generateArray ( arr , n ) { var ans = [ ] ; for ( var i = 0 ; i < n ; i ++ ) { if ( i & 1 ) ans . splice ( 0 , 0 , arr [ i ] ) ; else ans . push ( arr [ i ] ) ; } if ( n & 1 ) { ans . reverse ( ) ; } ans . forEach ( x => { document . write ( x + " " ) ; } ) ; } var n = 4 ; var arr = [ 1 , 2 , 3 , 4 ] ; generateArray ( arr , n ) ;