My issue
what is wrong in this ?
include
include
using namespace std;
int main() {
int t;
cin>>t;
while(t–){
int N,M;
cin>>N>>M;
int K[N];
for(int i = 0; i <N; i++){
cin>>K[i];
}
sort(K,K+N);
int a = 0;
int c=0;
for(int i = 0; i<N ; i++){
a = a+K[N-1-i];
c++;
if(a>=M){
cout<<c<<endl;
break;
}
}
if(a<M){
cout<<“-1”<<endl;
}
}
// your code goes here
return 0;
}
My code
#include <iostream>
#include<algorithm>
using namespace std;
int main() {
int t;
cin>>t;
while(t--){
int N,M;
cin>>N>>M;
int K[N];
for(int i = 0; i <N; i++){
cin>>K[i];
}
sort(K,K+N);
int a = 0;
int c=0;
for(int i = 0; i<N ; i++){
a = a+K[N-1-i];
c++;
if(a>=M){
cout<<c<<endl;
break;
}
}
if(a<M){
cout<<"-1"<<endl;
}
}
// your code goes here
return 0;
}
Learning course: Sorting using C++
Problem Link: CodeChef: Practical coding for everyone