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 element
step1: middle element 5 is shifted to front of array [5, 2, 8, 9, 10]
step2: middle element 8 is shifted to end of array [5, 2, 9, 10, 8]
step3: middle element 9 is shifted to front of array [9, 5, 2, 10, 8]
Input: arr[]=[10, 12, 6, 5, 3, 1]
Output: [1, 3, 5, 10, 6, 12]
Naive Approach: Shift the middle element of the array alternatively to the start and end of the array.
Take the middle element and shift it to the start of the Array if c is even or shift it to the end of Array if c is odd. Terminate the loop when the middle element becomes equal to the first element of the Original Array.
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
void AlternateShift(vector< int >& arr, int x)
{
int mid = arr.size() / 2;
int c = 0;
while (arr[mid] != x) {
int z = arr[mid];
arr.erase(arr.begin() + mid);
if (c % 2 == 0)
arr.insert(arr.begin() + 0, z);
else
arr.push_back(z);
c += 1;
}
}
int main()
{
vector< int > Arr = { 2, 8, 5, 9, 10 };
int a = Arr[0];
AlternateShift(Arr, a);
for ( int i = 0; i < Arr.size(); ++i) {
cout << Arr[i] << " " ;
}
return 0;
}
|
Java
import java.util.*;
class GFG {
static void alternateShift(ArrayList<Integer> arr, int x) {
int mid = arr.size() / 2 ;
int c = 0 ;
while (arr.get(mid) != x) {
int z = arr.get(mid);
arr.remove(mid);
if (c % 2 == 0 ) {
arr.add( 0 , z);
}
else {
arr.add(z);
}
c++;
}
}
public static void main(String[] args) {
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList( 2 , 8 , 5 , 9 , 10 ));
int a = arr.get( 0 );
alternateShift(arr, a);
for ( int i = 0 ; i < arr.size(); ++i) {
System.out.print(arr.get(i) + " " );
}
}
}
|
Python3
def AlternateShift(arr, x):
mid = len (arr) / / 2
c = 0
while arr[mid] ! = x:
z = arr.pop(mid)
if c % 2 = = 0 :
arr.insert( 0 , z)
else :
arr.append(z)
c + = 1
Arr = [ 2 , 8 , 5 , 9 , 10 ]
a = Arr[ 0 ]
AlternateShift(Arr, a)
print ( * Arr)
|
Javascript
<script>
const AlternateShift = (arr, x) => {
let mid = parseInt(arr.length / 2);
let c = 0;
while (arr[mid] != x) {
let z = arr[mid];
arr.splice(mid, 1);
if (c % 2 == 0)
arr.splice(0, 0, z);
else
arr.push(z);
c += 1;
}
}
Arr = [2, 8, 5, 9, 10];
let a = Arr[0];
AlternateShift(Arr, a);
for (let i = 0; i < Arr.length; ++i) {
document.write(`${Arr[i]} `);
}
</script>
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
public class GFG {
static void AlternateShift(List< int > arr, int x)
{
int mid = arr.Count / 2;
int c = 0;
while (arr[mid] != x) {
int z = arr[mid];
arr.RemoveAt(mid);
if (c % 2 == 0) {
arr.Insert(0, z);
}
else {
arr.Add(z);
}
c++;
}
}
static public void Main()
{
List< int > arr = new List< int >{ 2, 8, 5, 9, 10 };
int a = arr[0];
AlternateShift(arr, a);
for ( int i = 0; i < arr.Count; ++i) {
Console.Write(arr[i] + " " );
}
}
}
|
Time Complexity: O(n2)
Auxiliary Space: O(1)
Efficient Approach: Alternate Shifting is also the case of half reversal of array. First take an element from last to mid if n is even or take an element from last second to mid and insert into new array br[], then insert the first element into br[]. Then insert element from mid-1 to index 1 and insert into br[]. So it will return the array in half reversal order.
Algorithm :
step1: Declare new array br and initialize pos as n-1.
step2: If n is even traverse from last index pos or if n is odd then traverse from second last index pos-1.
step3: Store element from pos index to mid index into br array.
step4: Then insert first element of array to br array.
step5: If n is odd then insert last value elementof array to br array.
step6: Store element from mid-1 index to index 1 into br array.
step7: return br array.
Below is the implementation of the above algorithm :
C++
#include <iostream>
using namespace std;
int * rearrange( int * ar, int n)
{
int * br = new int [n];
int pos = n - 1;
if (n % 2 != 0)
pos = pos - 1;
int mid = n / 2;
int c = 0;
for (; pos >= mid; pos--)
br = ar[pos];
br = ar[0];
if (n % 2 != 0)
br = ar[n - 1];
for (; pos >= 1; pos--)
br = ar[pos];
return br;
}
int main()
{
int ar[] = { 2, 8, 5, 9, 10 };
int n = sizeof (ar) / sizeof (ar[0]);
int * res = rearrange(ar, n);
for ( int i = 0; i < n; i++)
cout << res[i] << " " ;
}
|
Java
import java.util.*;
class GFG
{
static int [] rearrange( int [] ar, int n)
{
int [] br = new int [n];
int pos = n - 1 ;
if (n % 2 != 0 )
pos = pos - 1 ;
int mid = n / 2 ;
int c = 0 ;
for (; pos >= mid; pos--)
br = ar[pos];
br = ar[ 0 ];
if (n % 2 != 0 )
br = ar[n - 1 ];
for (; pos >= 1 ; pos--)
br = ar[pos];
return br;
}
public static void main(String[] args)
{
int ar[] = { 2 , 8 , 5 , 9 , 10 };
int n = ar.length;
int [] res = rearrange(ar, n);
for ( int i = 0 ; i < n; i++)
System.out.print(res[i]+ " " );
}
}
|
Python3
def rearrange(ar, n):
br = [ 0 for i in range (n)]
pos = n - 1
if (n % 2 ! = 0 ):
pos = pos - 1
mid = n / / 2
c = 0
while (pos > = mid):
br = ar[pos]
c + = 1
pos - = 1
br = ar[ 0 ]
c + = 1
if (n % 2 ! = 0 ):
br = ar[n - 1 ]
c + = 1
while (pos > = 1 ):
br = ar[pos]
c + = 1
pos - = 1
return br
if __name__ = = '__main__' :
ar = [ 2 , 8 , 5 , 9 , 10 ]
n = len (ar)
res = rearrange(ar, n)
for i in range (n):
print (res[i],end = " " )
|
C#
using System;
public class GFG
{
static int [] rearrange( int [] ar, int n)
{
int [] br = new int [n];
int pos = n - 1;
if (n % 2 != 0)
pos = pos - 1;
int mid = n / 2;
int c = 0;
for (; pos >= mid; pos--)
br = ar[pos];
br = ar[0];
if (n % 2 != 0)
br = ar[n - 1];
for (; pos >= 1; pos--)
br = ar[pos];
return br;
}
public static void Main(String[] args)
{
int []ar = { 2, 8, 5, 9, 10 };
int n = ar.Length;
int [] res = rearrange(ar, n);
for ( int i = 0; i < n; i++)
Console.Write(res[i]+ " " );
}
}
|
Javascript
<script>
function rearrange(ar , n)
{
var br = Array.from({length: n}, (_, i) => 0);
var pos = n - 1;
if (n % 2 != 0)
pos = pos - 1;
var mid = parseInt(n / 2);
var c = 0;
for (; pos >= mid; pos--)
br = ar[pos];
br = ar[0];
if (n % 2 != 0)
br = ar[n - 1];
for (; pos >= 1; pos--)
br = ar[pos];
return br;
}
var ar = [ 2, 8, 5, 9, 10 ];
var n = ar.length;
var res = rearrange(ar, n);
for ( var i = 0; i < n; i++)
document.write(res[i]+ " " );
</script>
|
Time Complexity: O(n)
Space Complexity: O(n)
Last Updated :
30 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...