## PROBLEM LINK:

Setter: Shashwat Chandra

Tester: Yash Chandnani

Editorialist: Rajarshi Basu

# DIFFICULTY:

Easy

# PREREQUISITES:

Binary Representation

# PROBLEM:

Consider the following function, where `+`

denotes string concatenation.

```
function BinaryConcatenation(integer X, integer Y):
string binX = binary representation of X without leading zeroes
string binY = binary representation of Y without leading zeroes
string binXplusY = binX + binY
string binYplusX = binY + binX
integer XplusY = Convert binary representation binXplusY to integer
integer YplusX = Convert binary representation binYplusX to integer
return XplusY - YplusX
```

You are given a sequence A_1, A_2, \ldots, A_N, Find the maximum value of BinaryConcatenation(A_i, A_j) over all valid i and j.

# EXPLANATION:

## Observation 1

If you think about how the binary strings are written, the expression essentially boils down to the following

- (2^{MSB_Y}*X + Y) - (2^{MSB_X}*Y + X)

## Observation 2

Itâs always a good idea to fix some portion of the variables, and vary the others. What are reasonable choices to fix here?

## Answer

- Lets try to fix MSB_X and MSB_Y. There are a total of 30*30 choices for this.
What to do after this?

## Main Observation

Our Eqn now becomes:

- (A*X+Y)-(B*Y+X)
- = X*(A-1) - Y(B-1)

- A = 2^{MSB_Y}
- B = 2^{MSB_X}
Therefore, if we increase X, our solution doesnât become worse. Similarly, if we decrease Y our solution doesnât become worse. Hence, we can take the max value for X and the min value for Y.

For details on implementation, look at testerâs code.

# SOLUTION:

## Testerâs Code

```
#include <bits/stdc++.h>
using namespace std;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define repA(i, a, n) for(int i = a; i <= (n); ++i)
#define repD(i, a, n) for(int i = a; i >= (n); --i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a) memset(a, 0, sizeof (a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
void pre(){
}
ll f[2][31][31];
void solve(){
int n;cin>>n;
rep(i,2) rep(j,31) rep(k,31) f[i][j][k] = -(1ll<<60);
rep(i,n){
ll x;cin>>x;
int y = 64-__builtin_clzll(x);
repA(j,1,30){
f[0][y][j] = max(f[0][y][j],(x<<j)-x);
}
repA(j,1,30) f[1][j][y] = max(f[1][j][y],x-(x<<j));
}
ll ans = 0;
repA(i,1,30) repA(j,1,30) ans=max(ans,f[0][i][j]+f[1][i][j]);
cout<<ans<<'\n';
}
int main() {
cin.sync_with_stdio(0); cin.tie(0);
cin.exceptions(cin.failbit);
pre();
int n;cin>>n;
rep(i,n) solve();
return 0;
}
```