Submission #95098796 - Codeforces Please tell why is it giving a runtime error
Thanks
@anon79082028 Corrected code :
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin>>t;
while(t--){
int n,sum=0;
cin>>n;
int a[n]={};
for (int i=0,x;i<n;++i){
cin>>x;
a[i]=x;
sum+=x;
}
sort(a,a+n,greater<int>());
if (sum==0) cout<<"NO\n";
else{
cout<<"YES\n";
for (int i=0,x;i<n;++i){
cout<<a[i]<<" ";
}
cout<<'\n';
}
}
return 0;
}
Thank you
what about
1
5
2 1 0 -3 -4
here ur code will give YES and 2 1 0 -3 -4 but 2+1+0-3 is zero which is wrong
@mehul_23 I didn’t go through his whole logic. I only tried to remove runtime error from his code . Yes, but coming to your point, the code logic of @anon79082028 is definitely wrong.
I corrected that in my final code. I just couldn’t understand what was wrong with this one.
@mehul_23 @anon79082028 Here is the final AC code.
#include <bits/stdc++.h>
using namespace std;
void solve(){
int t, sum = 0;
cin >> t;
vector<int> arr(t);
for (int i = 0; i < t; ++i)
{
cin >> arr[i];
sum += arr[i];
}
if (!sum)
{
cout << "NO" << "\n";
return;
}
else {
cout << "YES" << "\n";
}
if (sum > 0)
sort(arr.rbegin() , arr.rend());
else
sort(arr.begin() , arr.end());
for (int i = 0; i < t; ++i)
{
cout << arr[i] << " ";
}
cout << "\n";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
int t;
cin >> t;
while(t--){
solve();
}
return 0;
}