We have an array of the form {a0, a1, a2….., an, b0, b1, b2, …..bn} and our task is to rearrange the same in theform given below by using O(1) space-
{a0, b0, a1, b1, a2, b2………..an, bn}
Examples:
Input : arr[] = {1, 3, 5, 7, 2, 4, 6, 8}
Output : {1, 2, 3, 4, 5, 6, 7, 8}
Input : arr[] = {11, 13, 15, 12, 14, 16}
Output : {11, 12, 13, 14, 15, 16}
We have already discussed two approaches-
As we can see we have to transform the array so there must be an even size array.To rearrange the array we will start from the middle of the array and each time we will shift 1 element of the second half to left at it’s desired position. This algorithm will take O(n^2).
Algorithm-
1- Take the input array
2- If size is null or odd return
3- find the middle index of the array
4- While (midindex>0)
set count = midindex and
swapindex = midindex
while (count-->0){
swap(swapindex+1, swapindedx)
swapindex++
}
midindex--
5- End
Working-
array- 1 3 5 7 2 4 6 8
1st Loop- 1 2 3 5 7 4 6 8
2nd Loop- 1 2 3 4 5 7 6 8
3rd loop- 1 2 3 4 5 6 7 8
C++
#include<iostream>
using namespace std;
void rearrange( int arr[], int n)
{
if (arr == NULL || n % 2 == 1)
return ;
int currIdx = (n - 1) / 2;
while (currIdx > 0)
{
int count = currIdx, swapIdx = currIdx;
while (count-- > 0)
{
int temp = arr[swapIdx + 1];
arr[swapIdx + 1] = arr[swapIdx];
arr[swapIdx] = temp;
swapIdx++;
}
currIdx--;
}
}
int main()
{
int arr[] = {1, 3, 5, 2, 4, 6};
int n = sizeof (arr) / sizeof (arr[0]);
rearrange(arr, n);
for ( int i = 0; i < n; i++)
cout << " " << arr[i];
}
|
Java
import java.io.*;
class GFG {
public static void rearrange( int [] arr) {
if (arr == null || arr.length % 2 == 1 )
return ;
int currIdx = (arr.length - 1 ) / 2 ;
while (currIdx > 0 ) {
int count = currIdx, swapIdx = currIdx;
while (count-- > 0 ) {
int temp = arr[swapIdx + 1 ];
arr[swapIdx + 1 ] = arr[swapIdx];
arr[swapIdx] = temp;
swapIdx++;
}
currIdx--;
}
}
public static void main(String[] args) {
int arr[] = { 1 , 3 , 5 , 2 , 4 , 6 };
rearrange(arr);
for ( int i = 0 ; i < arr.length; i++)
System.out.print( " " + arr[i]);
}
}
|
Python3
arr = [ 1 , 3 , 5 , 2 , 4 , 6 ]
def rearrange(n) :
global arr
if (n % 2 = = 1 ) :
return
currIdx = int ((n - 1 ) / 2 )
while (currIdx > 0 ) :
count = currIdx
swapIdx = currIdx
while (count > 0 ) :
temp = arr[swapIdx + 1 ]
arr[swapIdx + 1 ] = arr[swapIdx]
arr[swapIdx] = temp
swapIdx = swapIdx + 1
count = count - 1
currIdx = currIdx - 1
n = len (arr)
rearrange(n)
for i in range ( 0 , n) :
print ( "{} " . format (arr[i]),
end = "")
|
C#
using System;
class GFG {
public static void rearrange( int [] arr)
{
if (arr == null || arr.Length % 2 == 1)
return ;
int currIdx = (arr.Length - 1) / 2;
while (currIdx > 0) {
int count = currIdx, swapIdx = currIdx;
while (count-- > 0) {
int temp = arr[swapIdx + 1];
arr[swapIdx + 1] = arr[swapIdx];
arr[swapIdx] = temp;
swapIdx++;
}
currIdx--;
}
}
public static void Main() {
int []arr = {1, 3, 5, 2, 4, 6};
rearrange(arr);
for ( int i = 0; i < arr.Length; i++)
Console.Write( " " + arr[i]);
}
}
|
PHP
<?php
function rearrange(& $arr , $n )
{
if ( $arr == NULL ||
$n % 2 == 1)
return ;
$currIdx = intval (( $n - 1) / 2);
while ( $currIdx > 0)
{
$count = $currIdx ;
$swapIdx = $currIdx ;
while ( $count -- > 0)
{
$temp = $arr [ $swapIdx + 1];
$arr [ $swapIdx + 1] = $arr [ $swapIdx ];
$arr [ $swapIdx ] = $temp ;
$swapIdx ++;
}
$currIdx --;
}
}
$arr = array (1, 3, 5, 2, 4, 6);
$n = count ( $arr );
rearrange( $arr , $n );
for ( $i = 0; $i < $n ; $i ++)
echo ( $arr [ $i ] . " " );
?>
|
Javascript
<script>
function rearrange(arr, n)
{
if (arr == null || n % 2 == 1)
return ;
let currIdx = Math.floor((n - 1) / 2);
while (currIdx > 0)
{
let count = currIdx, swapIdx = currIdx;
while (count-- > 0)
{
let temp = arr[swapIdx + 1];
arr[swapIdx + 1] = arr[swapIdx];
arr[swapIdx] = temp;
swapIdx++;
}
currIdx--;
}
}
let arr = [ 1, 3, 5, 2, 4, 6 ];
let n = arr.length;
rearrange(arr, n);
for (let i = 0; i < n; i++)
document.write( " " + arr[i]);
</script>
|
Output:
1 2 3 4 5 6
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach:
Note: The assumption is that the largest value in the array is INT_MAX/2 and consists of only positive integers
The algorithm utilizes bit manipulation techniques to rearrange an array [x1,x2,…,xn,y1,y2,…,yn].
It is based on the constraint that 1 <= nums[i] <= INT_MAX/2.
- The largest number in the array is 1000 which requires 10 bits in binary representation.
- Thus, two numbers can fit into a 32-bit binary representation without overwriting each other.
- The algorithm has two loops: the first loop groups the numbers into pairs [x1, y1], [x2, y2]… [xn,yn] and stores both numbers in one binary representation.
- The second loop places these pairs in their final positions. To get the two numbers out of the binary representation, the algorithm uses Bitwise AND and Right Shift.
Implementation:
The algorithm has two parts, loop 1 and loop 2.
- Loop 1 groups the numbers in the array into pairs [x1, y1], [x2, y2]… [xn,yn] by storing both xn and yn in one binary representation.
- Loop 2 then places these pairs in their final position in the array.
To store two numbers in one binary representation, two pointers i and j are used.
i starts from n and moves backwards to 0, while j starts from the end of the array and moves backwards to n.
Code:
C++
#include<bits/stdc++.h>
using namespace std;
void rearrange( int nums[], int n)
{
n = n/2 ;
for ( int i = 0; i < n; i++){
nums[i] = nums[i] << 16 ;
nums[i] = nums[i] | nums[i+n] ;
}
int sixteen1s = pow (2, 16) - 1 ;
int j = 2*n - 1 ;
for ( int i = n-1; i >= 0 ; i--){
int y = nums[i] & sixteen1s ;
int x = nums[i] >> 16 ;
nums[j] = y ;
j-- ;
nums[j] = x ;
j-- ;
}
}
int main()
{
int arr[] = {1, 3, 5, 2, 4, 6};
int n = sizeof (arr) / sizeof (arr[0]);
rearrange(arr, n);
for ( int i = 0; i < n; i++)
cout << " " << arr[i];
}
|
Java
import java.util.Arrays;
class GFG {
static void rearrange( int [] nums, int n)
{
n = n / 2 ;
for ( int i = 0 ; i < n; i++) {
int x = nums[i] << 16 ;
nums[i] = x | nums[i + n];
}
int sixteen1s = ( 1 << 16 ) - 1 ;
int j = 2 * n - 1 ;
for ( int i = n - 1 ; i >= 0 ; i--) {
int y = nums[i] & sixteen1s;
int x = nums[i] >> 16 ;
nums[j] = y;
j--;
nums[j] = x;
j--;
}
}
public static void main(String[] args)
{
int [] arr = { 1 , 3 , 5 , 2 , 4 , 6 };
int n = arr.length;
rearrange(arr, n);
System.out.println(Arrays.toString(arr));
}
}
|
Python3
import math
def rearrange(nums, n):
n = n / / 2
for i in range (n):
nums[i] = nums[i] << 16
nums[i] = nums[i] | nums[i + n]
sixteen1s = pow ( 2 , 16 ) - 1
j = 2 * n - 1
for i in range (n - 1 , - 1 , - 1 ):
y = nums[i] & sixteen1s
x = nums[i] >> 16
nums[j] = y
j - = 1
nums[j] = x
j - = 1
if __name__ = = '__main__' :
arr = [ 1 , 3 , 5 , 2 , 4 , 6 ]
n = len (arr)
rearrange(arr, n)
for i in range (n):
print (arr[i], end = " " )
|
C#
using System;
using System.Collections.Generic;
class Gfg{
static void rearrange( int [] nums, int n)
{
n = n/2 ;
for ( int i = 0; i < n; i++){
nums[i] = nums[i] << 16 ;
nums[i] = nums[i] | nums[i+n] ;
}
int sixteen1s = ( int )Math.Pow(2, 16) - 1 ;
int j = 2*n - 1 ;
for ( int i = n-1; i >= 0 ; i--){
int y = nums[i] & sixteen1s ;
int x = nums[i] >> 16 ;
nums[j] = y ;
j-- ;
nums[j] = x ;
j-- ;
}
}
public static void Main(String[] args)
{
int [] arr = {1, 3, 5, 2, 4, 6};
int n = arr.Length;
rearrange(arr, n);
for ( int i = 0; i < n; i++)
Console.Write( " " + arr[i]);
}
}
|
Javascript
function rearrange(nums, n) {
n = Math.floor(n/2);
for (let i = 0; i < n; i++){
nums[i] = nums[i] << 16;
nums[i] = nums[i] | nums[i+n];
}
let sixteen1s = Math.pow(2, 16) - 1;
let j = 2*n - 1;
for (let i = n-1; i >= 0 ; i--)
{
let y = nums[i] & sixteen1s;
let x = nums[i] >> 16;
nums[j] = y;
j--;
nums[j] = x;
j--;
}
}
let arr = [1, 3, 5, 2, 4, 6];
let n = arr.length;
rearrange(arr, n);
for (let i = 0; i < n; i++)
console.log(arr[i]);
|
Complexity Analysis
- Time Complexity: O(N)
- Auxiliary Space: O(1)
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 geeks!
Last Updated :
16 Mar, 2023
Like Article
Save Article