BINFUN getting Runtime Error

For partially right, I used brute force but it gave runtime error.Why?
CODE::
#include
#include
#include <string.h>
using namespace std;

string intToBinaryString( long long int n )
{
char bArray[ (sizeof( long long int ) * 1024) + 1 ];

  long long int index  = sizeof( long long int ) * 1024;

  char temp =0;
  bArray[ index ] = '\0';

  do{          

        temp = (n & 1); 
        temp = temp + '0';
        bArray[ --index ] = temp;


  }while (n >>= 1);
  return string( bArray + index );

}

long long int BinaryConcatenation(long long int n1,long long int n2)
{

  std::string bString1 = intToBinaryString(n1);
   std::string bString2 = intToBinaryString(n2);
   

	string binX = 	 bString1.c_str();
	string binY =  bString2.c_str();
	//cout<<binX<<endl<<binY<<endl;
	
	string binXplusY = binX + binY;
    string binYplusX = binY + binX;
    //cout<<binXplusY<<endl<<binYplusX<<endl;
    
    long long int XplusY = 0, YplusX=0;
    XplusY = std :: stoi(binXplusY, 0, 2);
    YplusX = std :: stoi(binYplusX, 0, 2);
	
	//cout<<XplusY<<endl<<YplusX<<endl;
    
    long long int restemp = abs(XplusY - YplusX);
    return restemp;

}

int main(){
long long int t;
cin>>t;
while(t–)
{
long long int m,max=0;
cin>>m;
long long int arr[m];
for(long long int i=0;i<m;i++)
cin>>arr[i];

for(long long int i=0;i<m-1;i++)
    for(long long int j=i+1;j<m;j++)
        {
            long long int res1 = BinaryConcatenation(arr[i],arr[j]);
            if(res1 > max)
                max=res1;
        }
    
   cout<<max<<endl; 
    }

}

KINDLY help where i am getting wrong.

u tell answer.

Format your code first.

2 Likes

It’s probably the stoi(). I didn’t use it because in stackoverflow they mentioned that the argument must be an int but we deal with long long. So I wrote my own function to convert the string to a decimal.
My 50 points code:

#include <bits/stdc++.h>

using namespace std;

long long power(long long x, long long y)
{
    long long res = 1;
    while (y > 0)
    {
        if (y & 1) res *= x;
        y >>= 1;
        x *= x;
    }
    return res;
}

long long bd(string s)
{
    long long res = 0;
    for (long long i = s.size()-1, j = 0; i >= 0; --i, ++j) res += (long long)((long long)((int)s[i]-48) * power(2, j));
    return res;
}

string db(long long n)
{
    string s = "";
    while (n > 0) {
        s.push_back(n&1?'1':'0');
        n /= 2;
    }
    reverse(s.begin(), s.end());
    return s;
}

long long solve(long long x, long long y)
{
    string a = db(x);
    string b = db(y);
    return (bd(a+b)-bd(b+a));
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        vector <long long> a(n);
        long long ans = -1e15;
        for (int i = 0; i < n; ++i) cin >> a[i];
        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < n; ++j)
            {
                ans = max(ans, solve(a[i], a[j]));
            }
        }
        cout << ans << '\n';
    }
}
2 Likes

thanq i got it.

https://www.codechef.com/viewsolution/36008163

i am getting sigabrt error , can someone please find the mistake