visit
#include <bits/stdc++.h>
using namespace std;
/* function to seggregate negative
elements from the given array
and moving them to the end */
void segregate_elements(int n,vector<int> &arr){
vector<int> temp; //copy array
/* in the starting only copy the
non-negative elements */
for(int i=0;i<n;i++){
if(arr[i]>=0) temp.push_back(arr[i]);
}
/* copy the negative elements
to the end of dummy array */
for(int i=0;i<n;i++){
if(arr[i]<0) temp.push_back(arr[i]);
}
// copy the dummy array to the original array
for(int i=0;i<n;i++){
arr[i]=temp[i];
}
return;
}
int main() {
//input
int n;
cin>>n;
vector<int> arr(n);
for(int i=0;i<n;i++){
cin>>arr[i];
}
//segregating the array
segregate_elements(n,arr);
//output
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
return 0;
}
8
1 -1 3 2 -7 -5 11 6
#include <bits/stdc++.h>
using namespace std;
/* function to seggregate negative
elements from the given array
and moving them to the end */
void segregate_elements(int n,vector<int> &arr){
int low=0,high=n-1;
/* making high point to the last
non-negative integer in the array */
while(arr[high]<0 && high>low){
high--;
}
/* swap the low integer with high
whenever we find a negative integer */
while(low<high){
if(arr[low]<0){
swap(arr[low],arr[high]);
high--;
}
low++;
}
return;
}
int main() {
//input
int n;
cin>>n;
vector<int> arr(n);
for(int i=0;i<n;i++){
cin>>arr[i];
}
//segregating the array
segregate_elements(n,arr);
//output
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}
return 0;
}
8
1 -1 3 2 -7 -5 11 6