You are given an array of n distinct integers. Your task is to divide this array into three non-empty sets so as the following conditions hold:
- The first set should contain the elements such that their product should be less than 0.
- The Second set should contain the elements such that their product should be greater than 0.
- The Third set should contain the elements such that their product should be equal to 0.
- In the give array, each number must occur in exactly one set.
- It may be assumed that we can always make three sets (there is at-least one negative element and one 0 in input array).
Input : 4 arr = -1 -2 -3 0 Output : -1 -3 -2 0 In this example, product of first set is negative, product of second set is positive and product of third set is 0.
In this problem we just need to Split input data into 3 vectors: first will contain negative numbers, second positive numbers, third zeroes. If size of first vector is even move one number from it to the third vector. If second vector contains only 1, then move two numbers from first vector to the second vector.
-1 2 0
Time Complexity :- O(n)
- Only integer with positive value in positive negative value in array
- Partition negative and positive without comparison with 0
- Pairs of Positive Negative values in an array
- Minimum number of changes such that elements are first Negative and then Positive
- Segregating negative and positive maintaining order and O(1) space
- Print all the pairs that contains the positive and negative values of an element
- Rearrange positive and negative numbers using inbuilt sort function
- Rearrange positive and negative numbers in O(n) time and O(1) extra space
- Positive elements at even and negative at odd positions (Relative order not maintained)
- Lambda expression in Python to rearrange positive and negative numbers
- Longest alternating (positive and negative) subarray starting at every index
- Move all negative numbers to beginning and positive to end with constant extra space
- Replace all elements by difference of sums of positive and negative numbers after that element
- 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
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 GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.