GeeksforGeeks App
Open App
Browser
Continue

# Array obtained by repeatedly reversing array after every insertion from given array

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 3
Explanation:
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 2
Explanation:
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(N2)
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:

Below is the implementation of the above approach:

## C++

 `// C++ program of the above approach` `#include ``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;``}`

## 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 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`

## 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`

## 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`

## Javascript

 ``

Output:

`4 2 1 3`

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

My Personal Notes arrow_drop_up