Practice Rearrange Array Elements by Sign
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
nums has equal counts of positive and negative entries. Return all its entries rearranged to alternate signs, beginning positive, while preserving the original relative order within the positive entries and within the negative entries.
Source: Rearrange Array Elements by Sign on LeetCode. Use the original task for its coding environment and submissions.
Examples
# 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 <= 2 * 10^5
nums.length is even
1 <= |nums[i]| <= 10^5
nums consists of equal number of positive and negative integers.
Related LeetCode Problems
Practice more problems similar to Rearrange Array Elements by Sign.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.