-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path005.cpp
43 lines (39 loc) · 1.07 KB
/
005.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
// https://practice.geeksforgeeks.org/problems/move-all-negative-elements-to-end1813/1
void segregateElements(int arr[],int n)
{
// TLE Method 1: swapping postive to left side
// for (int i=0; i<n; i++){
// int j = i;
// while(arr[j] > 0 && j > 0 && arr[j-1] < 0){
// swap(arr[j], arr[j-1]);
// j--;
// }
// }
// TLE Method 2: swapping negative to right side
// for (int i=n-1; i>=0; i--){
// if (arr[i] < 0){
// int j = i;
// while(j < n-1 && arr[j+1] > 0){
// swap(arr[j], arr[j+1]);
// j++;
// }
// }
// }
// Method 3: traversing twice
vector<int> temp(n);
int index=0, tempIndex=0;
for (int i=0; i<n; i++){
if (arr[i] > 0){
temp[tempIndex] = arr[i];
tempIndex++;
}
}
for (int i=0; i<n; i++){
if (arr[i] < 0){
temp[tempIndex] = arr[i];
tempIndex++;
}
}
for (int i=0; i<n; i++)
arr[i] = temp[i];
}