Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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:
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] 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]. Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions.
Example 2:
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 * 105nums.length is even1 <= |nums[i]| <= 105nums consists of equal number of positive and negative integers.Problem summary: 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: Every consecutive pair of integers have opposite signs. For all integers with the same sign, the order in which they were present in nums is preserved. The rearranged array begins with a positive integer. Return the modified array after rearranging the elements to satisfy the aforementioned conditions.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Two Pointers
[3,1,-2,-5,2,-4]
[-1,1]
wiggle-subsequence)sort-array-by-parity-ii)partition-array-according-to-given-pivot)largest-number-after-digit-swaps-by-parity)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2149: Rearrange Array Elements by Sign
class Solution {
public int[] rearrangeArray(int[] nums) {
int[] ans = new int[nums.length];
int i = 0, j = 1;
for (int x : nums) {
if (x > 0) {
ans[i] = x;
i += 2;
} else {
ans[j] = x;
j += 2;
}
}
return ans;
}
}
// Accepted solution for LeetCode #2149: Rearrange Array Elements by Sign
func rearrangeArray(nums []int) []int {
ans := make([]int, len(nums))
i, j := 0, 1
for _, x := range nums {
if x > 0 {
ans[i] = x
i += 2
} else {
ans[j] = x
j += 2
}
}
return ans
}
# Accepted solution for LeetCode #2149: Rearrange Array Elements by Sign
class Solution:
def rearrangeArray(self, nums: List[int]) -> List[int]:
ans = [0] * len(nums)
i, j = 0, 1
for x in nums:
if x > 0:
ans[i] = x
i += 2
else:
ans[j] = x
j += 2
return ans
// Accepted solution for LeetCode #2149: Rearrange Array Elements by Sign
/**
* [2149] Rearrange Array Elements by Sign
*
* 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:
* <ol>
* Every consecutive pair of integers have opposite signs.
* For all integers with the same sign, the order in which they were present in nums is preserved.
* The rearranged array begins with a positive integer.
* </ol>
* 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]
* 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].
* Other ways such as [1,-2,2,-5,3,-4], [3,1,2,-2,-5,-4], [-2,3,-5,1,-4,2] are incorrect because they do not satisfy one or more conditions.
*
* Example 2:
*
* 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.
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/rearrange-array-elements-by-sign/
// discuss: https://leetcode.com/problems/rearrange-array-elements-by-sign/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn rearrange_array(nums: Vec<i32>) -> Vec<i32> {
vec![]
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2149_example_1() {
let nums = vec![3, 1, -2, -5, 2, -4];
let result = vec![3, -2, 1, -5, 2, -4];
assert_eq!(Solution::rearrange_array(nums), result);
}
#[test]
#[ignore]
fn test_2149_example_2() {
let nums = vec![-1, 1];
let result = vec![1, -1];
assert_eq!(Solution::rearrange_array(nums), result);
}
}
// Accepted solution for LeetCode #2149: Rearrange Array Elements by Sign
function rearrangeArray(nums: number[]): number[] {
const ans: number[] = Array(nums.length);
let [i, j] = [0, 1];
for (const x of nums) {
if (x > 0) {
ans[i] = x;
i += 2;
} else {
ans[j] = x;
j += 2;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.