Help in understanding the logic behind the problem [SOLVED]

I came across this problem, and i am having a little difficulty in solving the problem, can anyone help me where i am going wrong ?

My solution: I know this should give TLE but why is it giving wrong answer lol

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

#define ll long long

bool check(vector <int> &a, vector<int> &b) {
    set <int> s;
    for(int i=0;i<a.size();i++) {
        s.insert(a[i]+b[i]);
    }
    return (a.size() == s.size());
}

int main(){
    ios::sync_with_stdio(false); cin.tie(NULL);
    int t;
    cin >> t;
    while(t--) {
        int n;
        cin >> n;
        vector <int> a(n),b(n);
        for(int i=0;i<n;i++) {
            cin >> a[i];
        }
        for(int i=0;i<n;i++) {
            cin >> b[i];
        }
        bool exists =check(a,b);
        while(!exists) {
            random_shuffle(begin(a),end(a));
            exists = check(a,b);
        }
        for(int i=0;i<n;i++) {
            cout << a[i] << " ";
        }
        cout << '\n';
        for(int i=0;i<n;i++) {
            cout << b[i] << " ";
        }
        cout << '\n';
    }
}

I’m pretty much completely mystified, personally. Since it’s from an external contest, there’s actually a decent chance that the Problem itself is wrong XD

3 Likes

@cjinc_121 Can you help on how to solve this problem? Or atleast verify if the test cases are right ?

This problem looks very similar to a very recent codeforces problem.
All we had to do was just sort both arrays as it will always produce the distinct sums but when I tried that it gave WA. I think @ssjgz is correct about the problem being wrong.

2 Likes

Thanks @n_18bcs6626 . I also felt that i have seen the problem somewhere else. But when you mentioned codeforces, it became all clear. This problem was copied from A. Kuroni and the Gifts. I have implemented your solution and it gives AC on the original problem. As @ssjgz said, this problem is indeed wrong. Now this thread can be closed.

3 Likes

Anyone knows how to close a thread?

Just leave it open - it will fall off the front page soon enough :slight_smile:

I’ll add [Solved] to the title.

1 Like

ok thanks :slight_smile:

1 Like