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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void generateArray ( int arr [ ] , int n ) { deque < int > ans ; for ( int i = 0 ; i < n ; i ++ ) { if ( i & 1 ) ans . push_front ( arr [ i ] ) ; else ans . push_back ( arr [ i ] ) ; } if ( n & 1 ) { reverse ( ans . begin ( ) , ans . end ( ) ) ; } for ( auto x : ans ) { cout << x << " ▁ " ; } cout << endl ; } int32_t main ( ) { int n = 4 ; int arr [ n ] = { 1 , 2 , 3 , 4 } ; generateArray ( arr , n ) ; return 0 ; }