> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Rearrange Array Elements by Sign

> Tested Python solution for LeetCode 2149 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 2149, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Two Pointers](/catalog/topics/two-pointers), [Simulation](/catalog/topics/simulation). [View on LeetCode](https://leetcode.com/problems/rearrange-array-elements-by-sign/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2149   # by problem number
lcpy gen -s rearrange_array_elements_by_sign   # by problem name
```

## Problem

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 array follows the given conditions:

1. Every consecutive pair of integers have opposite signs.
2. For all integers with the same sign, the order in which they were present in nums is preserved.
3. The rearranged array begins with a positive integer.

Return the modified array after rearranging the elements to satisfy the aforementioned conditions.

### Examples

```
Input: nums = [3,1,-2,-5,2,-4]
Output: [3,-2,1,-5,2,-4]
```

**Explanation:** The positive integers in nums are \[3,1,2]. The negative integers are \[-2,-5,-4]. The only possible way to rearrange them such that they satisfy all conditions is \[3,-2,1,-5,2,-4].

```
Input: nums = [-1,1]
Output: [1,-1]
```

**Explanation:** 1 is the only positive integer and -1 the only negative integer in nums. So nums is rearranged to \[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.

It is not required to do the modifications in-place.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/rearrange_array_elements_by_sign/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/rearrange_array_elements_by_sign/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(n)
    def rearrange_array(self, nums: list[int]) -> list[int]:
        result = [0] * len(nums)
        pos_idx = 0
        neg_idx = 1
        for num in nums:
            if num > 0:
                result[pos_idx] = num
                pos_idx += 2
            else:
                result[neg_idx] = num
                neg_idx += 2
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.