
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Rearrange positive and negative numbers with constant extra space in C++
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
- Related Articles
- Rearrange positive and negative numbers in O(n) time and O(1) extra space in C++
- Rearrange array in alternating positive & negative items with O(1) extra space in C++
- Lambda expression in Python to rearrange positive and negative numbers
- Lambda expression in Python Program to rearrange positive and negative numbers
- Rearrange positive and negative numbers using inbuilt sort function in C++
- Distinguish positive and negative numbers.
- Implement Bubble sort with negative and positive numbers – JavaScript?
- Reversing negative and positive numbers in JavaScript
- Explain the difference between positive and negative numbers.
- Rearrange an array so that arr[i] becomes arr[arr[i]] with O(1) extra space using C++
- Check if array elements are consecutive in O(n) time and O(1) space (Handles Both Positive and negative numbers) in Python
- Distribute extra horizontal and vertical space in a GridBagLayout with Java
- Positive Testing and Negative Testing with Examples
- Count positive and negative numbers in a list in Python program
- Python program to count positive and negative numbers in a list
