# Rearrange array in alternating positive & negative items with O(1) extra space in C++

C++Server Side ProgrammingProgramming

We are given an integer type array containing both positive and negative numbers, let's say, arr[] of any given size. The task is to rearrange an array in such a manner that there will be a positive number that will be surrounded by negative numbers. If there are more positive and negative numbers, then they will be arranged at the end of an array.

## Let us see various input output scenarios for this −

Input − int arr[] = {-1, -2, -3, 1, 2, 3}

Output − Array before Arrangement: -1 -2 -3 1 2 3 Rearrangement of an array in alternating positive & negative items with O(1) extra space is: -1 1 -2 2 -3 3

Explanation − we are given an integer array of size 6 containing both positive and negative elements. Now, we will rearrange the array in such a manner that all the positive elements will be surrounded by the negative elements and all the extra elements will be added in the end of an array i.e -1 1 -2 2 -3 3 will be the final result.

Input − int arr[] = {-1, -2, -3, 1, 2, 3, 5, 5, -5, 3, 1, 1};

Output − Array before Arrangement: -1 -2 -3 1 2 3 5 5 -5 3 1 1 Rearrangement of an array in alternating positive & negative items with O(1) extra space is: -1 1 -2 2 -3 3 -5 5 5 3 1 1

Explanation − we are given an integer array of size 12 containing both positive and negative elements. Now, we will rearrange the array in such a manner that all the positive elements will be surrounded by the negative elements and all the extra elements will be added in the end of an array i.e -1 1 -2 2 -3 3 -5 5 5 3 1 1 will be the final result.

## Approach used in the below program is as follows

• Input an array of integer type elements and calculate the size of an array.

• Print an array before performing the rearrangement action using the FOR loop.

• Call to the function Rearrangement(arr, size) by passing array and size of an array as a parameter.

• Inside the function Rearrangement(arr, size)

• Declare an integer variable ‘ptr’ and initialize it with -1.

• Start loop FOR from i to 0 till i less than size. Inside the loop, check IF ptr greater than 0 then check IF arr[i] greater than 0 AND arr[ptr] less than 0 OR arr[i] less than 0 AND arr[ptr] greater than 0 then call to the function move_array(arr, size, ptr, i) and check IF i - ptr greater than 2 then set ptr to ptr + 2. ELSE, set ptr to -1.

• Check IF ptr to -1 then check arr[i] greater than 0 AND !(i & 0x01) OR (arr[i] less than 0) AND (i & 0x01) then set ptr to i.

• Inside the function move_array(int arr[], int size, int ptr, int temp)

• Declare a variable as ‘ch’ of type character and set it with arr[temp].

• Start loop FOR from i to temp till i greater than ptr. Inside the loop, set arr[i] with arr[i - 1].

• Set arr[ptr] to ch.

## Example

#include <iostream>
#include <assert.h>
using namespace std;
void move_array(int arr[], int size, int ptr, int temp){
char ch = arr[temp];
for(int i = temp; i > ptr; i--){
arr[i] = arr[i - 1];
}
arr[ptr] = ch;
}
void Rearrangement(int arr[], int size){
int ptr = -1;
for(int i = 0; i < size; i++){
if (ptr >= 0){
if(((arr[i] >= 0) && (arr[ptr] < 0)) || ((arr[i] < 0) && (arr[ptr] >= 0))){
move_array(arr, size, ptr, i);
if(i - ptr >= 2){
ptr = ptr + 2;
}
else{
ptr = -1;
}
}
}
if(ptr == -1){
if (((arr[i] >= 0) && (!(i & 0x01))) || ((arr[i] < 0) && (i & 0x01))){
ptr = i;
}
}
}
}
int main(){
//input an array
int arr[] = {-1, -2, -3, 1, 2, 3};
int size = sizeof(arr) / sizeof(arr);
//print the original Array
cout<<"Array before Arrangement: ";
for (int i = 0; i < size; i++){
cout << arr[i] << " ";
}
//calling the function to rearrange the array
Rearrangement(arr, size);
//print the array after rearranging the values
cout<<"\nRearrangement of an array in alternating positive & negative items with O(1) extra space is: ";
for(int i = 0; i < size; i++){
cout<< arr[i] << " ";
}
return 0;
}

## Output

If we run the above code it will generate the following Output

Array before Arrangement: -1 -2 -3 1 2 3
Rearrangement of an array in alternating positive & negative items with O(1) extra space is: -1 1 -2 2 -3 3