Rearrange positive and negative numbers in O(n) time and O(1) extra space in C++

C++Server Side ProgrammingProgramming

dd 5681,5653,4897,8144,4106,5338

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 all positive and negative numbers should be at alternate positions and if there are extra positive or negative elements then they will be placed in the end of an array.

Let us see various input output scenarios for this −

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

Output − Rearrangement of positive and negative numbers in O(n) time and O(1) extra space is: 2 - 1 6 -1 4 -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 and the negative elements are at an alternate positions and all the extra elements will be added in the end of an array i.e 2 -1 6 -1 4 -3 will be the final result

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

Output − Rearrangement of positive and negative numbers in O(n) time and O(1) extra space is: 2 - 2 3 -5 5 -3 5 -1 1 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 and the negative elements are at an alternate positions and all the extra elements will be added in the end of an array i.e 2 -2 3 -5 5 -3 5 -1 1 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 temporary integer type variables i.e.temp to -1, positive to temp + 1 and negative to 0.

    • Start loop FOR from i to 0 till i less than the size of an array. Inside the loop, check IF arr[i] less than 0 then increment the temp by 1 and call the inbuilt method of C++ STL i.e. swap(arr[temp], arr[i]) and pass arr[temp] and arr[i] as a parameter.

    • Start loop WHILE positive less than the size of an array AND negative less than the positive AND arr[negative] less than 0. Inside the loop, call swap by passing arr[negative] and arr[positive] as a parameter. Increment the positive by 1 and set negative to negative + 2.

  • Print the result.

Example

#include <bits/stdc++.h>
using namespace std;
void Rearrangement(int arr[], int size){
   int temp = -1;
   for(int i = 0; i < size; i++){
      if (arr[i] < 0){
         temp++;
         swap(arr[temp], arr[i]);
      }
   }
   int positive = temp + 1;
   int negative = 0;
   while(positive < size && negative < positive && arr[negative] < 0){
      swap(arr[negative], arr[positive]);
      positive++;
      negative = negative + 2;
   }
}
int main(){
   int arr[] = {4, 2, -1, -1, 6, -3};
   int size = sizeof(arr)/sizeof(arr[0]);
   //calling the function to rearrange the array
   Rearrangement(arr, size);
   //print the array after rearranging the values
   cout<<"Rearrangement of positive and negative numbers in O(n) time and 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

Rearrangement of positive and negative numbers in O(n) time and O(1) extra space is: 2 -1 6 -1 4 -3
raja
Updated on 02-Nov-2021 06:48:28

Advertisements