2149. Rearrange Array Elements by Sign LeetCode Solution

In this guide, you will get 2149. Rearrange Array Elements by Sign LeetCode Solution with the best time and space complexity. The solution to Rearrange Array Elements by Sign problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Rearrange Array Elements by Sign solution in C++
  4. Rearrange Array Elements by Sign solution in Java
  5. Rearrange Array Elements by Sign solution in Python
  6. Additional Resources
2149. Rearrange Array Elements by Sign LeetCode Solution image

Problem Statement of 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:

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.

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].

See also  906. Super Palindromes LeetCode Solution

Constraints:

2 <= nums.length <= 2 * 105
nums.length is even
1 <= |nums[i]| <= 105
nums consists of equal number of positive and negative integers.

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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

2149. Rearrange Array Elements by Sign LeetCode Solution in C++

class Solution {
 public:
  vector<int> rearrangeArray(vector<int>& nums) {
    vector<int> ans;
    vector<int> pos;
    vector<int> neg;

    for (const int num : nums)
      (num > 0 ? pos : neg).push_back(num);

    for (int i = 0; i < pos.size(); ++i) {
      ans.push_back(pos[i]);
      ans.push_back(neg[i]);
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

2149. Rearrange Array Elements by Sign LeetCode Solution in Java

class Solution {
  public int[] rearrangeArray(int[] nums) {
    int[] ans = new int[nums.length];
    List<Integer> pos = new ArrayList<>();
    List<Integer> neg = new ArrayList<>();

    for (final int num : nums)
      (num > 0 ? pos : neg).add(num);

    for (int i = 0; i < pos.size(); ++i) {
      ans[i * 2] = pos.get(i);
      ans[i * 2 + 1] = neg.get(i);
    }

    return ans;
  }
}
// code provided by PROGIEZ

2149. Rearrange Array Elements by Sign LeetCode Solution in Python

class Solution:
  def rearrangeArray(self, nums: list[int]) -> list[int]:
    ans = []
    pos = []
    neg = []

    for num in nums:
      (pos if num > 0 else neg).append(num)

    for p, n in zip(pos, neg):
      ans += [p, n]

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.