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
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
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Rearrange positive and negative numbers with constant 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
- 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
- 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
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.