Is the code not of O(n)
Time complexity of the programme
#include
using namespace std;
void swap(int &a,int &b){
int temp =a;
a=b;
b=temp;
}
int main() {
int n;
cin>>n;
int a[n+1];
int i=0,j=0;
while(j<n){
cin>>a[i];
if(a[i]==0||a[i]==1||a[i]==2){
i++;
}
j++;
}
int l=0,m=1,h=i-1;
while(m<=h){
if(a[m]==1)m++;
else if(a[m]==0){
swap(a[m],a[l]);
l++;
}
else{
swap(a[m],a[h]);
h–;
}
}
for(int k=0;k<i;k++){
cout<<a[k]<<endl;
}
}
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.