Given an array **arr[]**, the task is to print the array obtained by inserting elements of **arr[]** one by one into an initially empty array, say** arr1[]**, and reversing the array **arr1[]** after every insertion.

**Examples:**

Input:arr[] = {1, 2, 3, 4}Output:4 2 1 3Explanation:

Operations performed on the array arr1[] as follows:Step 1:Append 1 into the array and reverse it. arr1[] = {1}Step 2:Append 2 into the array and reverse it. arr1[] = {2, 1}Step 3:Append 3 into the array and reverse it. arr1[] = {3, 1, 2}Step 3:Append 4 into the array and reverse it. arr1[] = {4, 2, 1, 3}

Input:arr[] = {1, 2, 3}Output:3 1 2Explanation:

Operations performed on the array arr1[] as follows:Step 1:Append 1 into the array and reverse it. arr1[] = {1}Step 2:Append 2 into the array and reverse it. arr1[] = {2, 1}Step 3:Append 3 into the array and reverse it. arr1[] = {3, 1, 2}

**Naive Approach: **The simplest approach to solve the problem is to iterate over the array **arr[]** and insert each element of arr[] one by one into the array **arr1[]** and reverse the array **arr1[]** after each insertion.

**Time Complexity: **O(N^{2})**Auxiliary Space: **O(1)

**Efficient Approach: **To optimize the above approach, the idea is to use the Doubly Ended Queue (Deque) to append the elements at both ends. Follow the steps below to solve the problem:

- Initialize a Double Ended Queue for storing the elements of the converted array.
- Iterate over the elements of the array
**arr[]**and push the elements to the front of the Deque if the index of the element and length of the element have the same parity. Otherwise, at the push it to the back of the Deque. - Finally, after the complete iteration of the array
**arr[]**, print the contents of the Deque as the required arrangement of output.

Below is the implementation of the above approach:

## C++

`// C++ program of the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to generate the array by` `// inserting array elements one by one` `// followed by reversing the array` `void` `generateArray(` `int` `arr[], ` `int` `n)` `{` ` ` `// Doubly ended Queue` ` ` `deque<` `int` `> ans;` ` ` `// Iterate over the array` ` ` `for` `(` `int` `i = 0; i < n; i++) {` ` ` `// Push array elements` ` ` `// alternately to the front` ` ` `// and back` ` ` `if` `(i & 1)` ` ` `ans.push_front(arr[i]);` ` ` `else` ` ` `ans.push_back(arr[i]);` ` ` `}` ` ` `// If size of list is odd` ` ` `if` `(n & 1) {` ` ` `// Reverse the list` ` ` `reverse(ans.begin(),` ` ` `ans.end());` ` ` `}` ` ` `// Print the elements` ` ` `// of the array` ` ` `for` `(` `auto` `x : ans) {` ` ` `cout << x << ` `" "` `;` ` ` `}` ` ` `cout << endl;` `}` `// Driver Code` `int32_t main()` `{` ` ` `int` `n = 4;` ` ` `int` `arr[n] = { 1, 2, 3, 4 };` ` ` `generateArray(arr, n);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program of the above approach ` `import` `java.io.*;` `import` `java.util.*;` `class` `GFG{` ` ` `// Function to generate the array by` `// inserting array elements one by one` `// followed by reversing the array` `static` `void` `generateArray(` `int` `arr[], ` `int` `n)` `{` ` ` ` ` `// Doubly ended Queue` ` ` `Deque<Integer> ans = ` `new` `LinkedList<>();` ` ` ` ` `// Iterate over the array` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{` ` ` ` ` `// Push array elements` ` ` `// alternately to the front` ` ` `// and back` ` ` `if` `((i & ` `1` `) != ` `0` `)` ` ` `ans.addFirst(arr[i]);` ` ` `else` ` ` `ans.add(arr[i]);` ` ` `}` ` ` ` ` `// If size of list is odd` ` ` `if` `((n & ` `1` `) != ` `0` `) ` ` ` `{` ` ` ` ` `// Reverse the list` ` ` `Collections.reverse(Arrays.asList(ans)); ` ` ` `}` ` ` ` ` `// Print the elements` ` ` `// of the array` ` ` `for` `(` `int` `x : ans)` ` ` `{` ` ` `System.out.print(x + ` `" "` `);` ` ` `}` ` ` `System.out.println();` `}` ` ` `// Driver Code` `public` `static` `void` `main (String[] args)` `{` ` ` `int` `n = ` `4` `;` ` ` `int` `arr[] = { ` `1` `, ` `2` `, ` `3` `, ` `4` `};` ` ` ` ` `generateArray(arr, n);` `}` `}` `// This code is contributed by code_hunt` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program of the above approach ` `from` `collections ` `import` `deque` `# Function to generate the array by` `# inserting array elements one by one` `# followed by reversing the array` `def` `generateArray(arr, n):` ` ` ` ` `# Doubly ended Queue` ` ` `ans ` `=` `deque()` ` ` ` ` `# Iterate over the array` ` ` `for` `i ` `in` `range` `(n):` ` ` ` ` `# Push array elements` ` ` `# alternately to the front` ` ` `# and back` ` ` `if` `(i & ` `1` `!` `=` `0` `):` ` ` `ans.appendleft(arr[i])` ` ` `else` `:` ` ` `ans.append(arr[i])` ` ` ` ` `# If size of list is odd` ` ` `if` `(n & ` `1` `!` `=` `0` `):` ` ` ` ` `# Reverse the list` ` ` `ans.reverse()` ` ` ` ` `# Print the elements` ` ` `# of the array` ` ` `for` `x ` `in` `ans:` ` ` `print` `(x, end ` `=` `" "` `)` ` ` ` ` `print` `()` `# Driver Code` `n ` `=` `4` `arr ` `=` `[ ` `1` `, ` `2` `, ` `3` `, ` `4` `]` `generateArray(arr, n)` `# This code is contributed by code_hunt` |

*chevron_right*

*filter_none*

## C#

`// C# program of ` `// the above approach ` `using` `System;` `using` `System.Collections.Generic;` `class` `GFG{` ` ` `// Function to generate the array ` `// by inserting array elements ` `// one by one followed by ` `// reversing the array` `static` `void` `generateArray(` `int` `[]arr, ` ` ` `int` `n)` `{ ` ` ` `// Doubly ended Queue` ` ` `List<` `int` `> ans = ` `new` `List<` `int` `>();` ` ` `// Iterate over the array` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{` ` ` `// Push array elements` ` ` `// alternately to the front` ` ` `// and back` ` ` `if` `((i & 1) != 0)` ` ` `ans.Insert(0, arr[i]);` ` ` `else` ` ` `ans.Add(arr[i]);` ` ` `}` ` ` `// If size of list is odd` ` ` `if` `((n & 1) != 0) ` ` ` `{` ` ` `// Reverse the list` ` ` `ans.Reverse(); ` ` ` `}` ` ` `// Print the elements` ` ` `// of the array` ` ` `foreach` `(` `int` `x ` `in` `ans)` ` ` `{` ` ` `Console.Write(x + ` `" "` `);` ` ` `}` ` ` `Console.WriteLine();` `}` ` ` `// Driver Code` `public` `static` `void` `Main(String[] args)` `{` ` ` `int` `n = 4;` ` ` `int` `[]arr = {1, 2, 3, 4};` ` ` `generateArray(arr, n);` `}` `}` `// This code is contributed by 29AjayKumar` |

*chevron_right*

*filter_none*

**Output:**

4 2 1 3

**Time Complexity: **O(N)**Auxiliary Space: **O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- String obtained by reversing and complementing a Binary string K times
- Kth largest element after every insertion
- Find last two remaining elements after removing median of any 3 consecutive elements repeatedly
- Sum of array elements after reversing each element
- Largest number in given Array formed by repeatedly combining two same elements
- Find the final number obtained after performing the given operation
- Minimize array length by repeatedly replacing pairs of unequal adjacent array elements by their sum
- Maximum array sum that can be obtained after exactly k changes
- Minimize the sum calculated by repeatedly removing any two elements and inserting their sum to the Array
- Check whether an Array can be made 0 by splitting and merging repeatedly
- Array value by repeatedly replacing max 2 elements with their absolute difference
- Reduce array to a single element by repeatedly removing an element from any increasing pair
- Minimize Array length by repeatedly replacing co-prime pairs with 1
- Minimize remaining array element by repeatedly replacing pairs by half of one more than their sum
- Reduce array to a single element by repeatedly replacing adjacent unequal pairs with their maximum
- Largest string obtained in Dictionary order after deleting K characters
- Reduce number to a single digit by subtracting adjacent digits repeatedly
- Cost required to empty a given array by repeated removal of maximum obtained by given operations
- Sort the Array by reversing the numbers in it
- Reversing a Queue

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.