
- 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 in O(n) time and O(1) 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 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
- Related Articles
- Rearrange array in alternating positive & negative items with O(1) extra space in C++
- Rearrange positive and negative numbers with constant extra space in C++
- Find duplicates in O(n) time and O(1) extra space - Set 1 in C++
- Check if array elements are consecutive in O(n) time and O(1) space (Handles Both Positive and negative numbers) in Python
- Find the maximum repeating number in O(n) time and O(1) extra space in Python
- Count frequencies of all elements in array in O(1) extra space and O(n) time in C++
- Find maximum in a stack in O(1) time and O(1) extra space in C++
- Count Fibonacci numbers in given range in O(Log n) time and O(1) space in C++
- Find duplicate in an array in O(n) and by using O(1) extra space in C++
- Find median of BST in O(n) time and O(1) space in Python
- Find median of BST in O(n) time and O(1) space in C++
- Print left rotation of array in O(n) time and O(1) space in C Program.
- Print n x n spiral matrix using O(1) extra space in C Program.
- Reverse Individual Words With O(1) Extra Space
- Lambda expression in Python to rearrange positive and negative numbers
