-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay48.java
More file actions
54 lines (44 loc) · 2.82 KB
/
Copy pathDay48.java
File metadata and controls
54 lines (44 loc) · 2.82 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*Alternate Positive Negative
Given an unsorted array arr containing both positive and negative numbers. Your task is to rearrange the array and convert it into an array of alternate positive and negative numbers without changing the relative order.
Note:
- Resulting array should start with a positive integer (0 will also be considered as a positive integer).
- If any of the positive or negative integers are exhausted, then add the remaining integers in the answer as it is by maintaining the relative order.
- The array may or may not have the equal number of positive and negative integers.
Examples:
Input: arr[] = [9, 4, -2, -1, 5, 0, -5, -3, 2]
Output: [9, -2, 4, -1, 5, -5, 0, -3, 2]
Explanation: The positive numbers are [9, 4, 5, 0, 2] and the negative integers are [-2, -1, -5, -3]. Since, we need to start with the positive integer first and then negative integer and so on (by maintaining the relative order as well), hence we will take 9 from the positive set of elements and then -2 after that 4 and then -1 and so on.
Input: arr[] = [-5, -2, 5, 2, 4, 7, 1, 8, 0, -8]
Output: [5, -5, 2, -2, 4, -8, 7, 1, 8, 0]
Explanation : The positive numbers are [5, 2, 4, 7, 1, 8, 0] and the negative integers are [-5,-2,-8]. According to the given conditions we will start from the positive integer 5 and then -5 and so on. After reaching -8 there are no negative elements left, so according to the given rule, we will add the remaining elements (in this case positive elements are remaining) as it in by maintaining the relative order.
Input: arr[] = [9, 5, -2, -1, 5, 0, -5, -3, 2]
Output: [9, -2, 5, -1, 5, -5, 0, -3, 2]
Explanation: The positive numbers are [9, 5, 5, 0, 2] and the negative integers are [-2, -1, -5, -3]. Since, we need to start with the positive integer first and then negative integer and so on (by maintaining the relative order as well), hence we will take 9 from the positive set of elements and then -2 after that 5 and then -1 and so on. */
/*class Solution {
void rearrange(ArrayList<Integer> arr) {
ArrayList<Integer> pos = new ArrayList<>();
ArrayList<Integer> neg = new ArrayList<>();
// Step 1: Separate positives and negatives
for (int num : arr) {
if (num >= 0) pos.add(num);
else neg.add(num);
}
// Step 2: Merge alternately starting with positive
int i = 0, p = 0, n = 0;
while (p < pos.size() && n < neg.size()) {
arr.set(i++, pos.get(p++));
arr.set(i++, neg.get(n++));
}
// Step 3: Add remaining elements
while (p < pos.size()) {
arr.set(i++, pos.get(p++));
}
while (n < neg.size()) {
arr.set(i++, neg.get(n++));
}
}
}
*/
/*⏱ Time & Space Complexity
Time: O(n)
Space: O(n) (extra lists used) */