Given an array of positive and negative numbers, arrange them such that all negative integers appear before all the positive integers in the array without using any additional data structure like hash table, arrays, etc. The order of appearance should be maintained.
Input : arr = [12, 11, -13, -5, 6, -7, 5, -3, -6] Output : arr = [-13, -5, -7, -3, -6, 12, 11, 6, 5] Input : arr = [-12, 11, 0, -5, 6, -7, 5, -3, -6] Output : arr = [-12, -5, -7, -3, -6, 0, 11, 6, 5]
Previous Approaches : Some of the approaches have already been discussed here. They were implemented at best.
Approach 3: There is another method to do so. In c++ STL, There is an inbuilt function std::sort(). We can modify the comp() function to obtain the desired result. As we have to place negative numbers first and then positive numbers. We also have to keep zero’s(if present) between positive and negative numbers.
The comp() function in this code rearranges the given array in required order. Here in bool comp(int a, int b), if integer ‘a’ is of j-th index and integer ‘b’ is of i-th index elements in the arr, then j>i. comp() function will be called in this way. If the comp() return true then swap will be done.
-12 -13 -5 -7 -3 -6 11 6 5
-12 -13 -5 -7 -3 -6 11 6 5
Time complexity is same as sorting i.e. O(n log n). As we are using standard sort function. But it is really faster, because inbuilt sort function uses introsort.
Approach 4: There is yet another method to solve this problem. We recursively traverse the array cutting it into two halves (array[start..start] & array[(start + 1)..end], and keep on splitting the array till we reach the last element. Then we start merging it back. The idea is to, at any point, keep the array in proper sequence of negative and positive integers. The merging logic would be:
(I) If the array[start] is negative, merge the rest of the array as it is, so that the negative numbers’ order is maintained. The reason for this is that since we are tracing back from the recursive calls, we start moving right to left through the array, thus, naturally maintaining the original sequence.
(II) If the array[start] is positive, merge the rest of the array, but, after right-rotating the half of the array[(start + 1)..end]. The idea for the rotation is to merge the array so that the positive array[start] is always merged with the positive elements. But, the only thing here is that the merged array will have all the positive elements on the left and negative elements on the right. So we reverse the sequence in each recursion to get back the original sequence of negative elements and then positive elements subsequently.
It can be observed since we reverse the array while merging with a positive first element in each recursion, so the sequence of positive elements, although coming after the negative elements, are in a reverse order. So, as a final step, we reverse only the positive half of the final array, and, subsequently getting the intended sequence.
Below is the implementation of the above approach:
array: [-12, -11, -13, -5, -6, 7, 5, 3, 6] rearranged array: [-12, -11, -13, -5, -6, 7, 5, 3, 6]
Time complexity : O(N)
This article is contributed by abhijeet kaurav. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks 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.
- Rearrange positive and negative numbers with constant extra space
- Rearrange positive and negative numbers in O(n) time and O(1) extra space
- Lambda expression in Python to rearrange positive and negative numbers
- 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
- Bucket Sort To Sort an Array with Negative Numbers
- Replace all elements by difference of sums of positive and negative numbers after that element
- Move all negative numbers to beginning and positive to end with constant extra space
- Check if array elements are consecutive in O(n) time and O(1) space (Handles Both Positive and negative numbers)
- Sort an array without changing position of negative numbers
- Only integer with positive value in positive negative value in array
- Partition negative and positive without comparison with 0
- Minimum number of changes such that elements are first Negative and then Positive
- Pairs of Positive Negative values in an array
- Make three non-empty sets with negative, positive and 0 products