You are given a 0-indexed integer array nums of even length consisting of an equal number of positive and negative integers.
You should return the array of nums such that the the array follows the given conditions:
nums is preserved.Return the modified array after rearranging the elements to satisfy the aforementioned conditions.
Example 1:
Input: nums = [3,1,-2,-5,2,-4]
Output: [3,-2,1,-5,2,-4]Example 2:
Input: nums = [-1,1]
Output: [1,-1]Constraints:
2 <= nums.length <= 200,000nums.length is even.1 <= |nums[i]| <= 100,000nums consists of equal number of positive and negative integers.Before attempting this problem, you should be comfortable with:
We process the array position by position. At each index, we check if the current element has the correct sign (positive at even indices, negative at odd indices). If not, we search forward for an element with the correct sign and shift all elements in between to make room for it.
i:nums[i] > 0, or index is odd and nums[i] < 0, continue.j.nums[j], then shift all elements from i to j-1 one position right.i.class Solution:
def rearrangeArray(self, nums: List[int]) -> List[int]:
n = len(nums)
for i in range(n):
if ((i % 2 == 0 and nums[i] > 0) or
(i % 2 == 1 and nums[i] < 0)):
continue
j = i + 1
while j < n and ((nums[j] > 0) == (nums[i] > 0)):
j += 1
tmp = nums[j]
while j > i:
nums[j] = nums[j - 1]
j -= 1
nums[i] = tmp
return numsSince we need to alternate positive and negative numbers while preserving their relative order, we can first separate them into two lists. Then we interleave them back into the original array: positive numbers go to even indices, negative numbers go to odd indices.
pos for positive numbers and neg for negative numbers.pos[i] at index 2 * i.neg[i] at index 2 * i + 1.We can build the result in a single pass using two pointers. One pointer tracks the next even index (for positive numbers), and the other tracks the next odd index (for negative numbers). As we scan through the input, we place each number at the appropriate position and advance the corresponding pointer by 2.
i = 0 (for positive numbers at even indices) and j = 1 (for negative numbers at odd indices).res[i] and increment i by 2.res[j] and increment j by 2.res.A frequent mistake is mixing up which indices should hold positive versus negative numbers. Remember: even indices (0, 2, 4, ...) hold positive numbers, and odd indices (1, 3, 5, ...) hold negative numbers. Getting this reversed produces an invalid result.
The problem requires maintaining the relative order of positive numbers among themselves and negative numbers among themselves. Simply swapping elements in place without careful tracking can violate this constraint. Using separate lists or two-pointer placement ensures order is preserved.
While the problem guarantees no zeros in the input, some implementations incorrectly treat zero as positive (since 0 > 0 is false) or fail to account for the sign check properly. Ensure your sign comparison logic matches the problem constraints exactly.