java.lang.Object
g2101_2200.s2149_rearrange_array_elements_by_sign.Solution

public class Solution extends Object
2149 - Rearrange Array Elements by Sign.<p>Medium</p> <p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p> <p>You should <strong>rearrange</strong> the elements of <code>nums</code> such that the modified array follows the given conditions:</p> <ol> <li>Every <strong>consecutive pair</strong> of integers have <strong>opposite signs</strong>.</li> <li>For all integers with the same sign, the <strong>order</strong> in which they were present in <code>nums</code> is <strong>preserved</strong>.</li> <li>The rearranged array begins with a positive integer.</li> </ol> <p>Return <em>the modified array after rearranging the elements to satisfy the aforementioned conditions</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [3,1,-2,-5,2,-4]</p> <p><strong>Output:</strong> [3,-2,1,-5,2,-4]</p> <p><strong>Explanation:</strong> The positive integers in nums are [3,1,2].</p> <p>The negative integers are [-2,-5,-4].</p> <p>The only possible way to rearrange them such that they satisfy all conditions is [3,-2,1,-5,2,-4].</p> <p>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.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [-1,1]</p> <p><strong>Output:</strong> [1,-1]</p> <p><strong>Explanation:</strong> 1 is the only positive integer and -1 the only negative integer in nums.</p> <p>So nums is rearranged to [1,-1].</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 <= nums.length <= 2 * 10<sup>5</sup></code></li> <li><code>nums.length</code> is <strong>even</strong></li> <li><code>1 <= |nums[i]| <= 10<sup>5</sup></code></li> <li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • rearrangeArray

      public int[] rearrangeArray(int[] nums)