Rearrange given Array by splitting in half and inserting second half in reverse at alternate position
Last Updated :
10 Mar, 2022
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>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
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 3Explanation:Operations performed on the array a
6 min read
Rearrange given array such that no array element is same as its index Given an array arr[] consisting of N distinct integers, the task is to rearrange the array such that no element is same as its index ( 1-based indexing ). If multiple solutions exist, print any one of them. Examples: Input: arr[] = {4, 2, 3, 1}Output: 3 1 4 2Explanation: The elements at indices {1,
7 min read
Rearrange the Array by shifting middle elements to start and end alternatively Given an array, the task is to shift the middle element to the start and end of the array alternatively, till the middle element becomes equal to the first element of the original array. Input: arr[]=[2, 8, 5, 9, 10]Output: [9, 5, 2, 10, 8]Explanation: We can get this output by shifting middle eleme
13 min read
Alternate Rearrangement of Positives and Negatives An array contains both positive and negative numbers in random order. Rearrange the array elements so that positive and negative numbers are placed alternatively. A number of positive and negative numbers need not be equal. If there are more positive numbers they appear at the end of the array. If t
11 min read
Reverse an array upto a given position Given an array arr[] and a position in array, k. Write a function name reverse (a[], k) such that it reverses subarray arr[0..k-1]. Extra space used should be O(1) and time complexity should be O(k). Example: Input: arr[] = {1, 2, 3, 4, 5, 6} k = 4 Output: arr[] = {4, 3, 2, 1, 5, 6} We strongly reco
6 min read
Reverse the elements only at odd positions in the given Array Given an array arr[] containing N integers, the task is to rearrange the array such that the odd indexed elements are in reverse order. Examples: Input: arr[] = {5, 7, 6, 2, 9, 18, 11, 15} Output: {5, 15, 6, 18, 9, 2, 11, 7} Explanation: The elements at even index [5, 6, 9, 11] are unchanged and ele
6 min read