An array contains both positive and negative numbers in random order. Rearrange the array elements so that all negative numbers appear before all positive numbers.
Input: -12, 11, -13, -5, 6, -7, 5, -3, -6 Output: -12 -13 -5 -7 -3 -6 11 6 5
Note: Order of elements is not important here.
The idea is to simply apply the partition process of quicksort.
-1 -3 -7 4 5 6 2 8 9
Time complexity: O(N)
Auxiliary Space: O(1)
Two Pointer Approach: The idea is to solve this problem with constant space and linear time is by using a two-pointer or two-variable approach where we simply take two variables like left and right which hold the 0 and N-1 indexes. Just need to check that :
- Check If the left and right pointer elements are negative then simply increment the left pointer.
- Otherwise, if the left element is positive and the right element is negative then simply swap the elements, and Simultaneously increment or decrement the left and right pointers.
- Else if the left element is positive and the right element is also positive then simply decrement the right pointer.
- Repeat the above 3 steps until the left pointer ≤ right pointer.
Below is the implementation of the above approach:
-12 -3 -13 -5 -7 6 5 11 11
This is an in-place rearranging algorithm for arranging the positive and negative numbers where the order of elements is not maintained.
Time Complexity: O(N)
Auxiliary Space: O(1)
The problem becomes difficult if we need to maintain the order of elements. Please refer to Rearrange positive and negative numbers with constant extra space for details.
This article is contributed by Apoorva. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeek’s main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Rearrange positive and negative numbers with constant extra space
- Move all negative elements to end in order with extra space allowed
- Rearrange positive and negative numbers in O(n) time and O(1) extra space
- Rearrange array in alternating positive & negative items with O(1) extra space | Set 2
- Rearrange array in alternating positive & negative items with O(1) extra space | Set 1
- Check if array elements are consecutive in O(n) time and O(1) space (Handles Both Positive and negative numbers)
- Segregating negative and positive maintaining order and O(1) space
- Only integer with positive value in positive negative value in array
- Find ratio of zeroes, positive numbers and negative numbers in the Array
- Replace all elements by difference of sums of positive and negative numbers after that element
- Swap Kth node from beginning with Kth node from end in a Linked List
- C program to count Positive and Negative numbers in an Array
- Rearrange positive and negative numbers using inbuilt sort function
- Lambda expression in Python to rearrange positive and negative numbers
- Move all zeros to start and ones to end in an Array of random integers
- Print all the pairs that contains the positive and negative values of an element
- Count frequencies of all elements in array in O(1) extra space and O(n) time
- Move all zeroes to end of array
- Move all zeroes to end of array using List Comprehension in Python
- Move all zeroes to end of array | Set-2 (Using single traversal)
Improved By : nitin mittal, Pratik_Patil, chitranayal, anandkhatri, SumitSingh31, more