Cook-off July 2021 (CHFPLN) Correct Logic but code doesnt work

My code doesnt work dont know why.

[CHFPLN ](https://www.codechef.com/viewsolution/49213688)

#include<bits/stdc++.h>
#define nl “\n”
#define loop(n) for(int i = 0; i < n; i++)
#define fo(i, end) for(int i = 0; i < end; i++)
#define foo(i, begin, end) for(int i = begin; i < end; i++)
#define lld long long int

int B[200000];

using namespace std;

int main()
{
int T, N;
int count[100001] = {0};

ios_base::sync_with_stdio(false);
cin.tie(NULL);

cin>>T;
while(T--)
{
    cin>>N;
    int n = 0;
    int temp;
    fo(i,N)
    {
        cin>>temp;
        if(count[temp]==0)
        {
            B[n] = temp;
            count[temp]++;
            n++;
        }
        else
        {
            count[temp]++;
        }
    }
    
    int sum = 0;
    fo(i,n)
    {
        if(count[B[i]] < (B[i]-1))
        {
            sum += count[B[i]];
        }
        else
        {
            sum += B[i]-1;
        }
    }
    
    cout<<sum<<nl;
    
}

return 0;

}

i used an unordered map to deal with repeating values, accepted soln.

void soln() {
int t;
cin >> t;

while (t--) {
    int n;
    cin >> n;
    unordered_map<int, int> m;
    for (int i = 0; i < n; i++) {
        int temp;
        cin >> temp;
        m[temp]++;
    }
    int ans = 0;
    for (auto i : m) {
        if (i.second <= i.first - 1) {
            ans += i.second;
        } else {
            ans += i.first - 1;
        }
    }
    cout << ans << nl;
}

}

1 Like

you are using the previous test case data like in count[] and B[] array

2 Likes

Hi Sachin ,At first look your code is absolutely fine but I found that u have declared B[] and count[
] in wrong scope . As these value changes for each testcase u need to declare them inside each test case . Check this link I have submitted your code with the required changes and it works fine.
YourCodeWithModification

1 Like

Thanks bro got the problem now.
Looking at code I wasn’t able to figure out whats wrong.

1 Like