Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmRearrange given Array by splitting in half and inserting second half in...

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 4
Explanation: The first element is 1.
Then print the last element 6
Then the 2nd element 2 followed by the 2nd last element (i.e. the 5th element) 5.
Now print the 3rd element which is 3 and the 3rd last (i.e. the 4th element) 4.

Input: N = 4, arr[] = {12, 34, 11, 20}
Output: 12 20 34 11

 

Approach: This problem is solved by splitting the array into two parts with the help of two pointers i starting from 0 and j starting from (N-1). Now print the elements at i and j alternatively and increment i/ decrement j when an element at ith / jth position is inserted in new array.

Below is the implementation of the above approach.

C++




// C++ Program to implement the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to arrange array in alternate order
vector<int> printAlternate(int arr[], int N)
{
    int i = 0, j = N - 1;
    vector<int> ans;
 
    // Run loop while i <= j
    while (i <= j) {
 
        // Push the element from starting
        ans.push_back(arr[i]);
 
        // Push the element from back
        ans.push_back(arr[j]);
 
        // Increment i by 1
        i = i + 1;
 
        // Decrement j by 1
        j = j - 1;
    }
    return ans;
}
 
// Driver code
int main()
{
    int N = 6;
    int arr[N] = { 1, 2, 3, 4, 5, 6 };
 
    // Function call
    vector<int> ans = printAlternate(arr, N);
    for (int x : ans)
        cout << x << " ";
    return 0;
}


Java




// Java Program to implement the approach
import java.util.*;
 
class GFG {
 
  // Function to arrange array in alternate order
  public static ArrayList<Integer>
    printAlternate(int[] arr, int N)
  {
    int i = 0, j = N - 1;
    ArrayList<Integer> ans = new ArrayList<>();
 
    // Run loop while i <= j
    while (i <= j) {
 
      // Push the element from starting
      ans.add(arr[i]);
 
      // Push the element from back
      ans.add(arr[j]);
 
      // Increment i by 1
      i = i + 1;
 
      // Decrement j by 1
      j = j - 1;
    }
    return ans;
  }
 
  public static void main(String[] args)
  {
    int N = 6;
    int arr[] = { 1, 2, 3, 4, 5, 6 };
 
    // Function call
    ArrayList<Integer> ans = printAlternate(arr, N);
    for (int x : ans)
      System.out.print(x + " ");
  }
}
 
// This code is contributed by ninja_hattori.


Python3




# Python Program to implement the approach
# Function to arrange array in alternate order
 
def printAlternate(arr, N):
    i = 0
    j = N - 1
    ans = []
 
    # Run loop while i <= j
    while (i <= j):
 
        # Push the element from starting
        ans.append(arr[i])
 
        # Push the element from back
        ans.append(arr[j])
 
        # Increment i by 1
        i = i + 1
 
        # Decrement j by 1
        j = j - 1
 
    return ans
 
 
# Driver code
 
N = 6
arr = [1, 2, 3, 4, 5, 6]
 
# Function call
ans = printAlternate(arr, N)
for x in ans:
    print(x, end=' ')
 
# This code is contributed by Palak Gupta


C#




// C# Program to implement the approach
using System;
using System.Collections;
 
class GFG {
 
  // Function to arrange array in alternate order
  static ArrayList printAlternate(int[] arr, int N)
  {
    int i = 0, j = N - 1;
    ArrayList ans = new ArrayList();
 
    // Run loop while i <= j
    while (i <= j) {
 
      // Push the element from starting
      ans.Add(arr[i]);
 
      // Push the element from back
      ans.Add(arr[j]);
 
      // Increment i by 1
      i = i + 1;
 
      // Decrement j by 1
      j = j - 1;
    }
    return ans;
  }
 
  // Driver code
  public static void Main()
  {
    int N = 6;
    int[] arr = { 1, 2, 3, 4, 5, 6 };
 
    // Function call
    ArrayList ans = printAlternate(arr, N);
    foreach(int x in ans) Console.Write(x + " ");
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript




<script>
        // JavaScript code for the above approach
 
        // Function to arrange array in alternate order
        function printAlternate(arr, N)
        {
            let i = 0, j = N - 1;
            let ans = [];
 
            // Run loop while i <= j
            while (i <= j) {
 
                // Push the element from starting
                ans.push(arr[i]);
 
                // Push the element from back
                ans.push(arr[j]);
 
                // Increment i by 1
                i = i + 1;
 
                // Decrement j by 1
                j = j - 1;
            }
            return ans;
        }
 
        // Driver code
 
        let N = 6;
        let arr = [1, 2, 3, 4, 5, 6];
 
        // Function call
        let ans = printAlternate(arr, N);
        for (let x of ans)
            document.write(x + " ");
 
       // This code is contributed by Potta Lokesh
    </script>


Output

1 6 2 5 3 4 

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

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments