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)
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.
- Only integer with positive value in positive negative value in array
- Segregating negative and positive maintaining order and O(1) space
- Check if array elements are consecutive in O(n) time and O(1) space (Handles Both Positive and negative numbers)
- 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)
- Longest alternating (positive and negative) subarray starting at every index
- Partition negative and positive without comparison with 0
- Print all the pairs that contains the positive and negative values of an element
- Replace all elements by difference of sums of positive and negative numbers after that element
- Minimum number of changes such that elements are first Negative and then Positive
- Longest alternating subsequence in terms of positive and negative integers
- Find ratio of zeroes, positive numbers and negative numbers in the Array
- C program to count Positive and Negative numbers in an Array
- Maximum sum subset having equal number of positive and negative elements
- Maximum sum possible by assigning alternate positive and negative sign to elements in a subsequence
- Rearrange positive and negative numbers using inbuilt sort function
- Move all negative numbers to beginning and positive to end with constant extra space
- Lambda expression in Python to rearrange positive and negative numbers
- Pairs of Positive Negative values in an array
- Rearrange array in alternating positive & negative items with O(1) extra space | Set 2
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.