Move negative element

Practice

Author: Anurag
Tester: Anurag
Editorialist: Anurag

DIFFICULTY:

EASY,MEDIUM,GREEDY,STRING,CAKEWALK, SIMPLE

PREREQUISITES:

Math ,String

PROBLEM:

Hunter is given N element and he has to move all the negative element in left side array and print it .

EXPLANATION:

Hunter is given sum element and he has to move all the negative element to left side
lets take an example
suppose array of size 5 is given
2 -4 3 -1 5

in above example we have to move all the negative element to left side for this if we sort the array then automatically all the element move to left side
if we sort then -4 -1 2 3 5 array looks like it and this is our required answer

SOLUTIONS:

Setter's Solution

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
sort(a,a+n);
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}

cout<<endl;
}

return 0;
}

Tester's Solution

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
sort(a,a+n);
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}

cout<<endl;
}

return 0;
}

Editorialist's Solution

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
sort(a,a+n);
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}

cout<<endl;
}

return 0;
}