Skip to content
Decoder

Rearrange Array Elements by Sign

Medium

Problem

You are given a list nums of even length with equal numbers of positive and negative integers (no zeros). Return a new list where signs alternate, starting with a positive, and where the positives keep their original relative order and so do the negatives (preserve the order).

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 has the same number of positive and negative integers.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.