Rafi_2235's blog

By Rafi_2235, history, 4 months ago, In English

Hello guys, Recently I was trying to take preparation for google kickstart. And while solving a problem of kickstart-2020 I faced a problem with my solution. I was able to pass the sample tests but I was getting WA in test set 1. Can anyone please help me figure out the problem with my code. Here is my code-

#include <bits/stdc++.h>
using namespace std;

int solve(){
    int n,b;
    cin>>n>>b;
    int ar[n];
    for(int i=0;i<n;i++){
        cin>>ar[i];
    }
    sort(ar,ar+n);
    int a=0;
    int sm=ar[0];
    while(sm<=b){
        a+=1;
        sm+=ar[a];
    }
    return a;
}

int main(){
    int t,i=1;
    cin>>t;
    while(t--){
        int ans=solve();
        cout<<"Case #"<<i<<": "<<ans<<'\n';
        i++;
    }
    return 0;
}

*The problem link-- https://codingcompetitions.withgoogle.com/kickstart/round/000000000019ffc7/00000000001d3f56

 
 
 
 
  • Vote: I like it
  • -18
  • Vote: I do not like it

»
4 months ago, # |
  Vote: I like it 0 Vote: I do not like it
int a=0;
int sm=0;
while(sm+ar[a]<=b && a<n){
    sm+=ar[a];
    a+=1;
}

it will work now, hope you can understand your bug.

»
4 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Consider a case where B=100000 and A = [1,2,3,4,5].

Also just fyi, it often helps for you to explain the thinking behind your code since that makes it easier for others to debug.