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
- Only integer with positive value in positive negative value in array
- Minimum number of changes such that elements are first Negative and then Positive
- Pairs of Positive Negative values in an array
- Check if a number is positive, negative or zero using bit operators
- Make three non-empty sets with negative, positive and 0 products
- 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 in O(n) time and O(1) extra space
- Lambda expression in Python to rearrange positive and negative numbers
- Positive elements at even and negative at odd positions (Relative order not maintained)
- Longest alternating (positive and negative) subarray starting at every index
- Rearrange positive and negative numbers using inbuilt sort function
- Rearrange positive and negative numbers with constant extra space
- Replace all elements by difference of sums of positive and negative numbers after that element
- Move all negative numbers to beginning and positive to end with constant extra space
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.