Help me in solving GAME_XI problem

My issue

It is not passing hidden test cases

My approach :
I have sorted both array, reversed it, took there first four players (as they are the highest), erased them from the array.
concatenate the remaining array, sorted it, reversed it and then took the remaining 3 players
added all of their skills and printed it.
The visible test cases are passing but the hidden are not, please help me find the error

My code

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

int main() {
    int t;
    cin >> t;
    while (t--) {
        int N, M;
        cin >> N >> M;
        vector<int> arr_N(N);
        vector<int> arr_M(M);
        for (int &i : arr_N)
            cin >> i;
        for (int &i : arr_M)
            cin >> i;
        if (M < 4 || N < 4) {
            cout << "-1" << endl;
            continue;
        }

        sort(arr_N.begin(), arr_N.end());
        sort(arr_M.begin(), arr_M.end());

        reverse(arr_N.begin(), arr_N.end());
        reverse(arr_M.begin(), arr_M.end());

        int sum = 0;
        for (int i = 0; i < 4; i++) {
            sum += arr_N[i];
            sum += arr_M[i];
        }


        arr_N.erase(arr_N.begin(), arr_N.begin() + 4);
        arr_M.erase(arr_M.begin(), arr_M.begin() + 4);


        arr_N.insert(arr_N.end(), arr_M.begin(), arr_M.end());
        sort(arr_N.begin(), arr_N.end());
        reverse(arr_N.begin(), arr_N.end());


        for (int i = 0; i < 3; i++) {
            sum += arr_N[i];
        }

        cout << sum << endl;
    }

}


Problem Link: GAME 11 Practice Coding Problem - CodeChef

2 Likes

Your sum variable is a normal integer. It’s being overflowed.

Remember the numbers can be up to 10^9
So the sum of eleven 10^9 is 1.1*10^11, you need a long or a long long

As an advice, get used to always using long long whether you need it.

2 Likes

Thanks, will keep that advise in mind

I have made the changes of long long int and it passed 1 more hidden test case but it is still not fully correct.
Can you please help, I have just changed the int to long long int, and rest is same

After you input the data, you make an if to discard the test on criteria M<4 and N<4.

Both are ok, but you are missing one more to know if it’s a valid team.

After removing top 4 players from each array, you must all check if the remaining concatenated array has a minimum size of 3. Since 11 players are required to form a team.

Thnx, Got it

1 Like