Array obtained by repeatedly reversing array after every insertion from given array
Last Updated :
19 May, 2021
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 <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;
}
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
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
<script>
// Javascript program of the above approach
// Function to generate the array by
// inserting array elements one by one
// followed by reversing the array
function generateArray(arr, n)
{
// Doubly ended Queue
var ans = [];
// Iterate over the array
for (var i = 0; i < n; i++) {
// Push array elements
// alternately to the front
// and back
if (i & 1)
ans.splice(0,0,arr[i]);
else
ans.push(arr[i]);
}
// If size of list is odd
if (n & 1) {
// Reverse the list
ans.reverse();
}
// Print the elements
// of the array
ans.forEach(x => {
document.write( x + " ");
});
}
// Driver Code
var n = 4;
var arr = [1, 2, 3, 4];
generateArray(arr, n);
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Print the Array formed by reversing the given Array after each index Given an array arr[], the task is to print the array formed by traversing given array from first to the last index by flipping the whole array after printing every element. Example: Input: arr = {0, 1, 2, 3, 4, 5} Output: 0 4 2 2 4 0Explanation: On 1st iteration element on index 0 -> 0 is printed
6 min read
Find original array from given array which is obtained after P prefix reversals | Set-2 Given an array arr[] of size N and an integer P, the task is to find the original array from the array obtained by P prefix reversals where in ith reversal the prefix of size i of the array containing indices in range [0, i-1] was reversed. Note: P is less than or equal to N Examples: Input: arr[] =
7 min read
Find the original array from given array obtained after P prefix reversals Given an array arr[] of size N and an integer P (P < N), the task is to find the original array from the array obtained by P prefix reversals where in ith reversal the prefix of size i of the array containing indices in range [0, i-1] was reversed. Examples: Input: arr[] = {4, 2, 1, 3, 5, 6}, P =
10 min read
Restore the original array from another array generated by given operations Given an array b. The array b is obtained initially by array a, by doing the following operations. Choose a fixed point x from array a.Cyclically rotate the array a to the left exactly x times.Fixed point x is that point where (a[i] = i ). These operations are performed on the array a k times, deter
8 min read
Count remaining array elements after reversing binary representation of each array element Given an array arr[] consisting of N positive integers, the task is to modify every array element by reversing them binary representation and count the number of elements in the modified array that were also present in the original array. Examples: Input: arr[] = {2, 4, 5, 20, 16} Output: 2Explanati
8 min read
Rearrange given Array by splitting in half and inserting second half in reverse at alternate position Given an array arr[] of even length N, the task is to perform the following operations on the given array: Split the given array in half.Insert the second half in reverse order at alternate positions from the start. Examples: Input: N = 6, arr[] = {1, 2, 3, 4, 5, 6}Output: 1 6 2 5 3 4Explanation: Th
5 min read