

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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++
- Reversing negative and positive numbers in JavaScript
- Implement Bubble sort with negative and positive numbers – JavaScript?
- Positive Testing and Negative Testing with Examples
- Count positive and negative numbers in a list in Python program
- Java Program to convert positive int to negative and negative to positive
- Python program to count positive and negative numbers in a list
- Sum a negative number (negative and positive digits) - JavaScript
- Check if array elements are consecutive in O(n) time and O(1) space (Handles Both Positive and negative numbers) in Python
- Split an array of numbers and push positive numbers to JavaScript array and negative numbers to another?
- Distribute extra horizontal and vertical space in a GridBagLayout with Java