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


Array insertion and reversal are one of the most common array manipulation techniques. Array manipulation aims to modify an array's contents to get a desired outcome.

Problem Statement

Given an input array A[]. The task is to insert the elements of the given array into an existing array where a reversal of the output array follows each insertion.

Sample Example 1 −

Input: A[] = {1,2,3,4,5}
Output: R[] = {5, 3, 1, 2, 4}

Explanation

Initially, the output array R[] is empty.

  • Insertion of 1 : R[] = {1}

  • Insertion of 2 : R[] = {2, 1}

  • Insertion of 3 : R[] = {3, 1, 2}

  • Insertion of 4 : R[] = {4, 2, 1, 3}

  • Insertion of 5 : R[] = {5, 3, 1, 2, 4}

Sample Example 2 −

Input: A[] = {1, 4, 5, 11}
Output: R[] = {11, 4, 1, 5}

Explanation

Initially, the output array R[] is empty.

  • Insertion of 1: R[] = {1}

  • Insertion of 4: R[] = {4, 1}

  • Insertion of 5: R[] = {5, 1, 4}

  • Insertion of 11: R[] = {11, 4, 1, 5}

Approach 1: Brute Force Approach

The brute force and the most straightforward approach to the problem is to insert an element in the output array, then reverse the array and then insert the next element.

Pseudocode

function insertAndReverse(arr)
   result = empty vector
   for i from 0 to arr. size() - 1
      result.insert(result.begin(), arr[i])
      if i is not equal to (arr. size() - 1)
         reverse(result.begin(), result.end())
   return result

Example

Below is a C++ Implementation of the brute-force solution to the problem.

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

vector<int> insertAndReverse(vector<int> arr){
   vector<int> result;
   for (int i = 0; i < arr.size(); i++)    {
      result.insert(result.begin(), arr[i]);
      if (i != (arr.size() - 1))        {
         reverse(result.begin(), result.end());
      }
   }
   return result;
}
int main(){
   vector<int> given_array = {1, 2, 3, 4};
   vector<int> result_array = insertAndReverse(given_array);
   // Output the result array
   for (int num : result_array)    {
      cout << num << " ";
   }
   cout << endl;
   return 0;
}

Output

4 2 1 3

Time Complexity − O(N^2) as the reverse statement has time complexity of O(N) and the statement being inside a for loop results in O(N^2) time complexity.

Space Complexity − O(N)

Approach 2: Optimised Solution

Seeing the patterns of the result it can be concluded that the resultant array can be obtained by alternatively adding elements to the array from front and back.

The optimized solution to the problem can be obtained using this observation.

Pseudocode

function insertAndReverse(arr):
   result = empty vector
   for i from 0 to arr. size() - 1:
      if i is odd (i & 1 is not 0):
         result.insert(result.begin(), arr[i])
      else:
         result.insert(result.end(), arr[i])
   if arr.size() is odd (arr.size() & 1 is not 0):
      reverse(result.begin(), result.end())
   return result

Example

C++ Implementation

In the following program, the elements are pushed alternatively to the front and back of the array.

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

vector<int> insertAndReverse(vector<int> arr){
   vector<int> result;
   for (int i = 0; i < arr.size(); i++){
      if(i & 1){
         result.insert(result.begin(), arr[i]);
      } else {
         result.insert(result.end(), arr[i]);
      }
   }    
   if (arr.size() & 1){
      reverse(result.begin(), result.end());
   }
   return result;
}
int main(){
   vector<int> given_array = {1, 2, 3, 4};
   vector<int> result_array = insertAndReverse(given_array);
   // Output the result array
   for (int num : result_array){
      cout << num << " ";
   }
   cout << endl;
   return 0;
}

Output

4 2 1 3

Conclusion

In conclusion, the problem solved above is an array manipulation problem that can be solved using the best approach serving a time and space complexity of O(N) each.

Updated on: 25-Oct-2023

32 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements