-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy path11.cpp
More file actions
60 lines (58 loc) · 1.17 KB
/
Copy path11.cpp
File metadata and controls
60 lines (58 loc) · 1.17 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
55
56
57
58
59
60
//Rearrange the array in alternating positive and negative items with
//O(1) extra space.
#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for(int i=a;i<b;i++)
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define endl "\n"
#define mod 1000000007
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
void inputarr(int arr[], int n) {
REP(i, 0, n) {
cin >> arr[i];
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
int arr[n];
inputarr(arr, n);
//Algo1-O(n^2)
// REP(i, 0, n) {
// int j = i + 1;
// if (i % 2 == 0 && arr[i] < 0) {
// while (arr[j] < 0 && j < n) {j++;}
// if (arr[j] >= 0)
// swap(arr[i], arr[j]);
// } else if (i % 2 != 0 && arr[i] >= 0) {
// while (arr[j] >= 0 && j < n) {j++;}
// if (arr[j] < 0)
// swap(arr[i], arr[j]);
// }
// }
//Algo2-O(n)
int i = 0, j = n - 1;
while (i < j) {
while (arr[i] >= 0)i++;
while (arr[j] < 0)j--;
if (i < j)
swap(arr[i], arr[j]);
}
//i has the index of leftmost -ve element
int k = 0;
while (i < n && k < n) {
swap(arr[i], arr[k]);
i++;
k += 2;
}
REP(i, 0, n) {
cout << arr[i] << " ";
}
}