Rearrange Array Elements by Sign - Complete Solution Guide
Rearrange Array Elements by Sign is LeetCode problem 2149, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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: 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 aforem
Detailed Explanation
The problem requires rearranging an array of integers, `nums`, which has an equal number of positive and negative elements. The rearranged array must alternate between positive and negative numbers, starting with a positive number. The relative order of positive numbers and negative numbers must be preserved from the original array.
Solution Approach
The solution creates a new array called `result` of the same size as the input array `nums`. It then iterates through `nums`. If an element is positive, it's placed in `result` at the next available even index. If it's negative, it's placed at the next available odd index. The even and odd indices are tracked using `pos_idx` and `neg_idx` respectively, each incrementing by 2 after placement.
Step-by-Step Algorithm
- Step 1: Initialize a new array `result` of the same size as `nums`, filled with zeros.
- Step 2: Initialize two index pointers: `pos_idx` to 0 (for placing positive numbers at even indices) and `neg_idx` to 1 (for placing negative numbers at odd indices).
- Step 3: Iterate through the input array `nums`.
- Step 4: For each number `num` in `nums`, check if it is positive (num > 0).
- Step 5: If `num` is positive, place it at `result[pos_idx]` and increment `pos_idx` by 2.
- Step 6: If `num` is negative, place it at `result[neg_idx]` and increment `neg_idx` by 2.
- Step 7: After iterating through all numbers in `nums`, return the `result` array.
Key Insights
- Insight 1: Since there's an equal number of positive and negative integers, and the rearranged array has to start with a positive integer and alternate signs, the placement of positive and negative numbers is deterministic (positive at even indices, negative at odd indices).
- Insight 2: The problem emphasizes maintaining the original order of positive and negative numbers separately. This hints at keeping track of the positions for positive and negative elements as we iterate through the original array.
- Insight 3: A new array must be created to store the rearranged numbers, since the modification is not required to be in-place.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Array, Two Pointers, Simulation.
Companies
Asked at: Infosys.