Given an array of n integers, both negative and positive, partition them into two different arrays without comparing any element with 0.
Input : arr = [1, -2, 6, -7, 8] Output : a = [1, 6, 8] b = [-2, -7]
- Initialize two empty vectors. Push the first element of the array in any of the two vectors, suppose the first vector. Let it be denoted by x
- For every other element, arr to arr[n-1], check if its sign and the sign of x is same or not. If the signs are the same, then push the element in the same vector. Else, push the element in the other vector
- After the traversal of the two vectors has completed, print both the vectors
How to check if their signs are opposite or not?
Let the integers to be checked be denoted by x and y. The sign bit is 1 in negative numbers, and 0 in positive numbers. The XOR of x and y will have the sign bit as 1 if and only if they have opposite signs. In other words, XOR of x and y will be a negative number if and only if x and y have opposite signs.
1 6 8 -2 -7
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
- Minimum number of changes such that elements are first Negative and then Positive
- Check if a number is positive, negative or zero using bit operators
- Pairs of Positive Negative values in an array
- C program to count Positive and Negative numbers in an Array
- Segregating negative and positive maintaining order and O(1) space
- Make three non-empty sets with negative, positive and 0 products
- Print all the pairs that contains the positive and negative values of an element
- Rearrange positive and negative numbers using inbuilt sort function
- Lambda expression in Python to rearrange 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 subsequence in terms of positive and negative integers
- Rearrange positive and negative numbers with constant extra space
- Maximum sum subset having equal number of positive and negative elements
- Longest alternating (positive and negative) subarray starting at every index
- Rearrange array in alternating positive & negative items with O(1) extra space | Set 1
- Rearrange array in alternating positive & negative items with O(1) extra space | Set 2
- 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
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.