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)
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- 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
- Print all the pairs that contains the positive and negative values of an element
- Segregating negative and positive maintaining order and O(1) space
- C program to count Positive and Negative numbers in an Array
- Rearrange positive and negative numbers using inbuilt sort function
- Longest alternating subsequence in terms of positive and negative integers
- 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)
- Maximum sum subset having equal number of positive and negative elements
- Longest alternating (positive and negative) subarray starting at every index
- Lambda expression in Python to rearrange positive and negative numbers
- Move all negative numbers to beginning and positive to end with constant extra space
- Rearrange array in alternating positive & negative items with O(1) extra space | Set 1
- 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
- Check if array elements are consecutive in O(n) time and O(1) space (Handles Both Positive and negative numbers)
- Make two sets disjoint by removing minimum elements
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.