-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalternatePostiveNegative.cpp
More file actions
58 lines (58 loc) · 1.44 KB
/
alternatePostiveNegative.cpp
File metadata and controls
58 lines (58 loc) · 1.44 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
#include<iostream>
#include<vector>
#include<algorithm>
void rightRotate(std::vector<int> &A,int left, int right){
if(right==A.size()){
//no need to do anything
return;
}
int temp = A[right];
int index = right;
while(index-1>=left){
A[index] = A[index-1];
--index;
}
A[left] = temp;
}
std::vector<int> solve(std::vector<int> &A){
for(int index = 0;index<A.size();++index){
if(index%2!=0){
//index for non negative number
if(A[index]<0){
int j = index+1;
while(j<A.size() && A[j]<0){
//find the nonNegative Number
++j;
}
//index j has the non negative number
rightRotate(A,index,j);
}
}
else{
//index for negative number
if(A[index]>=0){
int j = index+1;
while(j<A.size() && A[j]>=0){
//find the nonNegative Number
++j;
}
//index j has the non negative number
rightRotate(A,index,j);
}
}
}
return A;
}
int main(){
int N;
std::cin>>N;
std::vector<int> vec(N);
for(int index = 0;index<N;++index){
std::cin>>vec[index];
}
std::vector<int> output = solve(vec);
for(int &x: output){
std::cout<<x<<" ";
}
return 0;
}