My issue
My code is failing task#4. When I debugged my code it showed that my code is failing at input
1
2
6 5
but my output is correct for that input.
0
5 6
what is the issue?
My code
#include <bits/stdc++.h>
using namespace std;
int f(int x,int y){
return x>=y;
}
int main() {
int t;
cin>>t;
while(t--){
int n,sum=0,c=0,s=0;
cin>>n;
vector<int>a,b;
// map<int,int>m;
for(int i=0;i<n;i++){
int x;
cin>>x;
// a.push_back(x);
b.push_back(x);
}
sort(b.begin(),b.end());
// swap(a[0],*min_element(a.begin(),a.end()));
for(int i=0;i<n-1;i++){
int l=0,r=n;
sum+=b[i];
while(r-l>1){
int m=(l+r)/2;
if(f(sum,b[m])) l=m;
else r=m;
}
if(r==n) break;
else swap(b[i+1],b[r]);
}
for(int i=0;i<n;i++){
s+=b[i];
a.push_back(s);
}
for(int i=1;i<n;i++){
if(a[i-1]>=b[i]) c++;
}
cout<<c<<endl;
for(auto x:b)cout<<x<<" ";cout<<endl;
}
return 0;
}
Problem Link: Cursed Indices Practice Coding Problem - CodeChef