Array obtained by repeatedly reversing array after every insertion from given array | C # program of the above approach ; 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
using System ; using System . Collections . Generic ; class GFG { static void generateArray ( int [ ] arr , int n ) { List < int > ans = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( i & 1 ) != 0 ) ans . Insert ( 0 , arr [ i ] ) ; else ans . Add ( arr [ i ] ) ; } if ( ( n & 1 ) != 0 ) { ans . Reverse ( ) ; } foreach ( int x in ans ) { Console . Write ( x + " ▁ " ) ; } Console . WriteLine ( ) ; } public static void Main ( String [ ] args ) { int n = 4 ; int [ ] arr = { 1 , 2 , 3 , 4 } ; generateArray ( arr , n ) ; } }