Rearrange positive and negative numbers with constant 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 all the elements of an array are sorted using the inbuilt sort function of C++ STL as well as using recursive technique of coding and printing the result.

Let us see various input output scenarios for this −

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

Output − Rearrangement of positive and negative numbers with constant extra space is: -3 -1 -1 0 6 2 4.

Explanation − we are given an integer array of size 7 containing both positive and negative elements. Now, we will rearrange the array in such a manner that all the elements of an array are sorted within the constant extra space and the final result will be -3 -1 -1 0 2 4 6.

Input − int arr[] = {-9, -10, 2, 3, 10, 5, 8, 4}

Output − Rearrangement of positive and negative numbers with constant extra space is: -9 -10 2 3 10 5 8 4

Explanation − we are given an integer array of size 8 containing both positive and negative elements. Now, we will rearrange the array in such a manner that all the elements of an array are sorted within the constant extra space and the final result will be -9 -10 2 3 10 5 8 4.

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 type variable as i to 0 and j to size -1.

    • Start while(true). Inside the while, start another loop while arr[i] less than 0 AND i less than size then increment the i by 1.

    • Start WHILE arr[j] greater than 0 AND j greater than 0 then decrement the j by 1.

    • Check IF i less than j then set temp to arr[i], arr[i] to arr[j] and arr[j] to temp.

    • ELSE, break.

  • Print the result.

Example

#include<iostream>
using namespace std;
void Rearrangement(int arr[], int size){
   int i = 0;
   int j = size - 1;
   while(true){
      while(arr[i] < 0 && i < size){
         i++;
      }
      while(arr[j] > 0 && j >= 0){
         j--;
      }
      if (i < j){
         int temp = arr[i];
         arr[i] = arr[j];
         arr[j] = temp;
      }
      else{
         break;
      }
   }
}
int main(){
   int arr[] = {4, 2, -1, -1, 6, -3, 0};
   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 with constant 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 with constant extra space is: -3 -1 -1 0 6 2 4
raja
Published on 02-Nov-2021 06:54:58
Advertisements