<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 return the array of nums such that the the 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>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [3,1,-2,-5,2,-4]
<strong>Output:</strong> [3,-2,1,-5,2,-4]
<strong>Explanation:</strong>
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.  
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [-1,1]
<strong>Output:</strong> [1,-1]
<strong>Explanation:</strong>
1 is the only positive integer and -1 the only negative integer in nums.
So nums is rearranged to [1,-1].
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li>
	<li><code>nums.length</code> is <strong>even</strong></li>
	<li><code>1 &lt;= |nums[i]| &lt;= 10<sup>5</sup></code></li>
	<li><code>nums</code> consists of <strong>equal</strong> number of positive and negative integers.</li>
</ul>

<p>&nbsp;</p>
It is not required to do the modifications in-place.
class Solution {
public:
    vector<int> rearrangeArray(vector<int>& nums) 
{
    // Dekho bhaiya, basic idea yeh hai ki array mein equal number of positive aur negative elements hai,
    // toh hum do alag arrays banaenge, ek mein positive aur doosre mein negative elements rakh lenge.
    // Jaise order main elements pehle array mein hai, waise hi rakhenge.
    // Phir final result ke liye hum even index pe positive element daalenge aur odd index pe negative element.
    
    int n = nums.size();
    
    // s1 positive elements ke liye aur s2 negative elements ke liye
    int s1 = n / 2, s2 = n - s1;
    
    // Do alag arrays banate hain, ek positive ke liye aur doosra negative ke liye
    int arr1[s1];  // Positive elements ke liye
    int arr2[s2];  // Negative elements ke liye
    
    // Do pointers banate hain, p positive ke liye aur k negative ke liye
    int p = 0, k = 0;

    // Pehle loop mein positive aur negative elements ko alag-alag arrays mein daalenge
    for (int i = 0; i < nums.size(); i++)
    {
        if (nums[i] > 0)  
        {
            arr1[p++] = nums[i];  // Positive elements ko arr1 mein daal do
        }
        else
        {
            arr2[k++] = nums[i];  // Negative elements ko arr2 mein daal do
        }
    }

    // Ab jo initial vector diya gaya tha, usko clear kar do
    nums.clear();

    // Ab final nums array ko bharte hain, alternating positions par positive aur negative rakhte hue
    // Dono arrays ke iterators ko 0 par laate hain
    p = 0;
    k = 0;
    
    // Ab final vector ko alternating positive aur negative elements se bharte hain
    for (int i = 0; i < (s1 + s2); i++)
    {
        if (i % 2 == 0)  // Even index pe positive element daalo
        {
            nums.push_back(arr1[p++]);
        }
        else  // Odd index pe negative element daalo
        {
            nums.push_back(arr2[k++]);
        }
    }

    // Final rearranged array return kar do
    return nums;
}

};
127202