Help in Binary Concatenation

/************

Author :- Profesor(Abhishek)
2020-07-25-19.42.44
**********/
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define fast ios_base::sync_with_stdio(0);cin.tie(0);
#define endl “\n”
#define fori(i,a,b) for(ll i=a;i<b;i++)
#define forr(i,a,b) for(ll i=a;i>=b;i–)
#define forit(it,x) for (auto it=(x).begin();it!=(x).end(); it++)
#define all(x) (x).begin(),(x).end()
#define allr(x) (x).rbegin(),(x).rend()
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sll set
#define vll vector
#define msl map<string,ll>
#define mll map<ll,ll>

unsigned countBits(unsigned ll number)
{
// log function in base 2
// take only integer part
return (ll)log2(number)+1;
}

ll i, j, k;
int main()
{
fast;
ll t;
cin >> t;
while(t–)
{
ll n;
cin >> n;
ll arr[n];
fori(i,0,n)
{
cin >> arr[i];
}
if(n==1)
{
cout << arr[0] << endl;
continue;
}
sort(arr , arr+n);

    ll xplusy, yplusx ,maximum = INT_MIN , x , y;

    fori(i, 0, n-1)
    {
        fori(j,i+1,n)
        {
            xplusy=pow(2, countBits(arr[j]))*arr[i]+arr[j];
            yplusx = pow(2,countBits(arr[i]))*arr[j]+arr[i];
            ll ans = xplusy - yplusx;
            ans=abs(ans);
            if( ans > maximum)
            {
                maximum = ans;
                x= arr[i];
                y=arr[j];

            }
        }
    }
    cout << maximum << endl;

// cout << x << endl << y << endl;

}
return 0;

}

2 Likes

J loop from 0-n not I+1 to n

I am not able to understand its time complexity.
Isnt this approach taking N2 time(which is not compatible for N=1e5) ?

1 Like

after this too it’s giving wrong anwere now
https://www.codechef.com/viewsolution/36039663

not asking for tle just asking why it’s giving wrong ans.

Problem is with your pow() function.It is creating some precision errors and if you typecast your pow with (ll)pow() the code works out fine(partially ofc…).I don’t know the exact reason behind this precision error but you can take a look at this…
pow-function-returning-wrong-result

Yes, but it always work fine for power of 2 that’s why i used it.