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++
#include <bits/stdc++.h>
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;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static void generateArray( int arr[], int n)
{
Deque<Integer> ans = new LinkedList<>();
for ( int i = 0 ; i < n; i++)
{
if ((i & 1 ) != 0 )
ans.addFirst(arr[i]);
else
ans.add(arr[i]);
}
if ((n & 1 ) != 0 )
{
Collections.reverse(Arrays.asList(ans));
}
for ( int x : ans)
{
System.out.print(x + " " );
}
System.out.println();
}
public static void main (String[] args)
{
int n = 4 ;
int arr[] = { 1 , 2 , 3 , 4 };
generateArray(arr, n);
}
}
|
Python3
from collections import deque
def generateArray(arr, n):
ans = deque()
for i in range (n):
if (i & 1 ! = 0 ):
ans.appendleft(arr[i])
else :
ans.append(arr[i])
if (n & 1 ! = 0 ):
ans.reverse()
for x in ans:
print (x, end = " " )
print ()
n = 4
arr = [ 1 , 2 , 3 , 4 ]
generateArray(arr, n)
|
C#
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);
}
}
|
Javascript
<script>
function generateArray(arr, n)
{
var ans = [];
for ( var i = 0; i < n; i++) {
if (i & 1)
ans.splice(0,0,arr[i]);
else
ans.push(arr[i]);
}
if (n & 1) {
ans.reverse();
}
ans.forEach(x => {
document.write( x + " " );
});
}
var n = 4;
var arr = [1, 2, 3, 4];
generateArray(arr, n);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...