WA in Binary Concatenation

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

int BinaryConcatenation(int x,int y){
    string binx = bitset<30>(x).to_string();
    binx = binx.erase(0, min(binx.find_first_not_of('0'), binx.size()-1));
    string biny = bitset<30>(y).to_string();
    biny = biny.erase(0, min(biny.find_first_not_of('0'), biny.size()-1));
    string binxplusy = binx+biny;
    string binyplusx = biny+binx;
    //int xplusy = stoi(binxplusy, nullptr, 2);
    //int yplusx = stoi(binyplusx, nullptr, 2);
    unsigned long long xplusy = bitset<60>(binxplusy).to_ullong();
    unsigned long long yplusx = bitset<60>(binyplusx).to_ullong();
    return (xplusy - yplusx);
}

int main() {
	int t;
    scanf("%d",&t);
    while(t--){
        int n,i,j,max=INT_MIN,res;
        scanf("%d",&n);
        int a[n];
        for(i=0;i<n;i++){
            scanf("%d",&a[i]);
        }
        for(i=0;i<n;i++){
            for(j=i;j<n;j++){
                res=BinaryConcatenation(a[i],a[j]);
                if(res>max){
                    max=res;
                }
            }
        }
        printf("%d\n",max);
    }
	return 0;
}

this is my code and it is working fine for the given test cases but gives wrong answer after submission , i am only trying to pass subtask 1 through this but am not able to

Answer may not fit in int
I have never used bitset. I think there is some issue in that. Also, you should start j with 0 or always return absolute value in function. You may see my sol
https://www.codechef.com/viewsolution/35987947

1 Like

You aren’t considering the permutation variants F(A,B) and F(B,A) are negatives of each other. But you are always considering F(A_i,A_j) and ignoring F(A_j,A_i)=-F(A_i,A_j). This will give WA when F(A_i,A_j) less than zero

1 Like

i tried starting j from 0 and returning abs value from function but still WA …
i checked your solution and understood your approach but still want to know where am i going wrong…

Thanks, but still WA …

Your AC sol
https://www.codechef.com/viewsolution/36042074
Problem was in function when you were returning. I told you to take absolute value but since you have defined it as unsigned and x-y will give garbage value when y>x. So, taking its abs will not make it correct.

1 Like

Thankyou so much ! do you have any suggestion that how can i solve TLE for subtask 2 in this solution…

Have you read the editorial ?