-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRearrangeArrayElementsBySign.java
33 lines (32 loc) · 1.12 KB
/
RearrangeArrayElementsBySign.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.namanh.two_pointers;
/**
* https://leetcode.com/problems/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 rearrange the elements of nums such that the modified 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.
*
* S1: Iterate array
* S2: If num > 0, push it to even index
* S3: If num < 0, push it to odd index
*
* Time: O(n)
* Space: O(n)
*/
public class RearrangeArrayElementsBySign {
public int[] rearrangeArray(int[] nums) {
int n = nums.length, posIdx = 0, negIdx = 1;
int[] result = new int[n];
for (int num : nums) {
if (num > 0) {
result[posIdx] = num;
posIdx += 2;
} else {
result[negIdx] = num;
negIdx += 2;
}
}
return result;
}
}