I am new in CodeChef and need some help in one of my solutions.
I have come up with below solution for the BINFUN problem from July Lunchtime:
https://www.codechef.com/viewsolution/36047870
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll highestBinDiff(ll numbers[], ll size);
int main() {
try{
ll tc = 0;
ll num = 0;
cin >> tc;
while(tc--){
num = 0;
cin >> num;
ll arr[num];
for(ll idx = 0; idx < num; idx++){
cin >> arr[idx];
}
cout<<highestBinDiff(arr, num)<<endl;
}
}catch(exception e){
cout<<e.what()<<endl;
}
return 0;
}
ll highestBinDiff(ll numbers[], ll size)
{
ll maxBinDiff = 0;
unordered_map<ll, pair<ll, ll> > hashTable;
for(ll idx = 0; idx < size; idx++)
{
ll num = numbers[idx];
ll pos = log2(num) + 1;
if(hashTable.find(pos) == hashTable.end()){
hashTable[pos] = {num, num};
}
else{
hashTable[pos].first = min(hashTable[pos].first, num);
hashTable[pos].second = max(hashTable[pos].second, num);
}
}
for(int i = 0; i < 30; i++)
{
if(hashTable.find(i) == hashTable.end())
{
continue;
}
for(int j = 0; j < 30; j++)
{
if(hashTable.find(j) == hashTable.end())
{
continue;
}
ll x = hashTable[i].second;
ll y = hashTable[j].first;
ll diff = abs((x * pow(2, j) + y) - (y * pow(2, i) + x));
maxBinDiff = max(diff, maxBinDiff);
}
}
hashTable.clear();
return maxBinDiff;
}
Not being able to understand why it is not working. Will appreciate any leads.