My issue
can anyone explain me the third test-case here why the answer is 4 instead of 5.
My code
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long n,c;
cin>>n>>c;
long long sum=0;
vector<long long >arr(n);
for(int i=0;i<n;i++){
cin>>arr[i];
sum+=arr[i];
}
sort(arr.begin(),arr.end());
long long ans=n;
for(int i=0;i<n;i++){
sum=sum-arr[i];
long long b=sum*arr[i];
c=c-b;
if(c<0){break;}
ans--;
}
if(ans==0){
ans=1;
}
cout<<ans<<endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
Problem Link: Destroying Bridges Part 2 Practice Coding Problem