-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrangingTheArray.cpp
55 lines (52 loc) · 1.51 KB
/
ArrangingTheArray.cpp
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
55
/*
You are given an array of size N. Rearrange the given array in-place such that all the negative numbers occur before all non-nagative numbers.(Maintain the order of all -ve and non-negative numbers as given in the original array).
Example 1:
Input:
N = 4
Arr[] = {-3, 3, -2, 2}
Output:
-3 -2 3 2
Explanation:
In the given array, negative numbers
are -3, -2 and non-negative numbers are 3, 2.
Example 1:
Input:
N = 4
Arr[] = {-3, 1, 0, -2}
Output:
-3 -2 1 0
Explanation:
In the given array, negative numbers
are -3, -2 and non-negative numbers are 1, 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function Rearrange() which takes the array Arr[] and its size N as inputs and returns the array after rearranging with spaces between the elements of the array.
Expected Time Complexity: O(N. Log(N))
Expected Auxiliary Space: O(Log(N))
*/
class Solution
{
public:
void Rearrange(int arr[], int n)
{
vector<int> t1,t2;
for(int i=0;i<n;i++){
if(arr[i]<0){
t1.push_back(arr[i]);
}else{
t2.push_back(arr[i]);
}
}
int i=0, j=0;
while(j<t1.size()){
arr[i]=t1[j];
i++;
j++;
}
j=0;
while(j<t2.size()){
arr[i]=t2[j];
i++;
j++;
}
}
};