304 North Cardinal St.
Dorchester Center, MA 02124

# Binary Concatenation CodeChef Solution

## Binary Concatenation CodeChef Solution in C++17

``````#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
//#include<map>
//#include<stack>
//#include<queue>
//#include<deque>
//#include<vector>
//#include<list>
//#include<set>
#define ll long long
#define db double
#define sg string
#define ud unsigned
using namespace std;
//#define int long long
//#include<bits/stdc++.h>

/*struct node{
ll be,en;
bool operator<(const node &n)const{
return en>n.en;
}
};*/

//freopen(".in","r",stdin);freopen(".out","w",stdout);
//fclose(stdin);fclose(stdout);

inline ll read(){ll sum=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();} while(ch>='0'&&ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0'; ch=getchar();}return sum*f;}
inline ll qpow(ll x,ll y,ll p){ll sum=1; while(y){ if(y&1){ sum=sum*x%p; } y>>=1; x=x*x%p;} return sum;}

const ll N=5e2+10;
ll t=rd;
int main(){
while(t--){
ll n=rd,li[40],la[40],ans=0;
memset(li,0x3f,sizeof(li));
memset(la,0,sizeof(li));
for(int i=1;i<=n;i++){
ll a=rd,l=log2(a)+1;

li[l]=min(a,li[l]);
la[l]=max(a,la[l]);
}
for(int l1=1;l1<=30;l1++){
for(int l2=1;l2<=30;l2++){
if(li[l2]>1e10){
continue;
}
ans=max(ans,la[l1]*((1<<l2)-1)-li[l2]*((1<<l1)-1));
}
}
cout<<ans<<endl;
}
return 0;
}``````

## Binary Concatenation CodeChef Solution in C++14

``````#include <bits/stdc++.h>
using namespace std;
long long pows[61]= {0};
long long conc(long long a,long long b)
{
double _l1=log2l(b);
long l1=ceil(_l1) + (floor(_l1)==ceil(_l1));
double _l2=log2l(a);
long l2=ceil(_l2) + (floor(_l2)==ceil(_l2));
long num1=a*pows[l1] + b;
long num2=b*pows[l2] + a;
return num1-num2;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
long test44;
cin >> test44;
pows[0]=1;
for(int i=1; i<=60; i++)
{
pows[i]=pows[i-1]*2;
}
while(test44--)
{
long n;
cin >> n;
long a[n],temp[n];
for(long i = 0 ; i < n ; i++)
{
cin >> a[i];
temp[i]=a[i];
}
long res=0;
sort(temp,temp+n,greater<long>());
for(long i = 0 ; i < min(2L,(long)n) ; i++)
{
for(long long j = 0 ; j < n ; j++)
{
long cur = conc(temp[i],a[j]);
long cur2 = conc(a[j],temp[i]);
res=max(res,max(cur2,cur));
}
}
cout << res << '\n';
}
return 0;
}``````

## Binary Concatenation CodeChef Solution in PYTH 3

``````#Smallest number closest to power of 2 on negative side largest and largest
#pow(2,len(o))-1 *n - (pow(2,len(n))-1)*o
#number closest to power of 2 on positive side
#
from collections import defaultdict
def binaryConcat(x,y):
xplusy=int(bin(x)[2:]+bin(y)[2:],2)
yplusx=int(bin(y)[2:]+bin(x)[2:],2)
return(abs(xplusy-yplusx))
def solution(arr):
dmax=defaultdict(int)
dmin=defaultdict(int)
for i in range(len(arr)):
blen=len(bin(arr[i]))-2
if(dmax[blen]==0 or dmax[blen]<arr[i]):
dmax[blen]=arr[i]
if(dmin[blen]==0 or dmin[blen]>arr[i]):
dmin[blen]=arr[i]
maxv=-float('inf')
for i in dmax.keys():
for j in dmin.keys():
bcat=binaryConcat(dmax[i],dmin[j])
if(bcat>maxv):
maxv=bcat
return maxv

#print(diffNegative(1))
t=int(input())
for _ in range(t):
n=input()
arr=list(map(int,input().split()))
print(solution(arr))
``````

## Binary Concatenation CodeChef Solution in C

``````//set many funcs template
//Ver.20190820
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>
#include<time.h>
#include<assert.h>
#define inf 1072114514
#define llinf 4154118101919364364
#define mod 1000000007
#define pi 3.1415926535897932384

int max(int a,int b){if(a>b){return a;}return b;}
int min(int a,int b){if(a<b){return a;}return b;}
int zt(int a,int b){return max(a,b)-min(a,b);}
int round(int a,int b){if((a%b)*2 >= b){return (a/b)+1;}return a/b;}
int ceil(int a,int b){if(a%b==0){return a/b;}return (a/b)+1;}
int gcd(int a,int b){int c;while(b!=0){c=a%b;a=b;b=c;}return a;}
int lcm(int a,int b){int c=gcd(a,b);a/=c;return a*b;}
int nCr(int a,int b){int i,r=1;for(i=1;i<=b;i++){r*=(a+1-i);r/=i;}return r;}
int nHr(int a,int b){return nCr(a+b-1,b);}
int fact(int a){int i,r=1;for(i=1;i<=a;i++){r*=i;}return r;}
int pow(int a,int b){int i,r=1;for(i=1;i<=b;i++){r*=a;}return r;}
int dsum(int x){int r=0;while(x){r+=(x%10);x/=10;}return r;}
int dsumb(int x,int b){int r=0;while(x){r+=(x%b);x/=b;}return r;}
int sankaku(int x){return ((1+x)*x)/2;}
void swap(int *a,int *b){int c;c=(*a);(*a)=(*b);(*b)=c;}
long long llmax(long long a,long long b){if(a>b){return a;}return b;}
long long llmin(long long a,long long b){if(a<b){return a;}return b;}
long long llzt(long long a,long long b){return llmax(a,b)-llmin(a,b);}
long long llround(long long a,long long b){if((a%b)*2 >= b){return (a/b)+1;}return a/b;}
long long llceil(long long a,long long b){if(a%b==0){return a/b;}return (a/b)+1;}
long long llgcd(long long a,long long b){long long c;while(b!=0){c=a%b;a=b;b=c;}return a;}
long long lllcm(long long a,long long b){long long c=llgcd(a,b);a/=c;return a*b;}
long long llnCr(long long a,long long b){long long i,r=1;for(i=1;i<=b;i++){r*=(a+1-i);r/=i;}return r;}
long long llnHr(long long a,long long b){return llnCr(a+b-1,b);}
long long llfact(long long a){long long i,r=1;for(i=1;i<=a;i++){r*=i;}return r;}
long long llpow(long long a,long long b){long long i,r=1;for(i=1;i<=b;i++){r*=a;}return r;}
long long lldsum(long long x){long long r=0;while(x){r+=(x%10);x/=10;}return r;}
long long lldsumb(long long x,long long b){long long r=0;while(x){r+=(x%b);x/=b;}return r;}
long long llsankaku(long long x){return ((1+x)*x)/2;}
void llswap(long long *a,long long *b){long long c;c=(*a);(*a)=(*b);(*b)=c;}
double dbmax(double a,double b){if(a>b){return a;}return b;}
double dbmin(double a,double b){if(a<b){return a;}return b;}
double dbzt(double a,double b){return dbmax(a,b)-dbmin(a,b);}
void dbswap(double *a,double *b){double c;c=(*a);(*a)=(*b);(*b)=c;}
void chswap(char *a,char *b){char c;c=(*a);(*a)=(*b);(*b)=c;}
int sortfncsj(const void *a,const void *b){if(*(int *)a>*(int *)b){return 1;}if(*(int *)a==*(int *)b){return 0;}return -1;}
int sortfnckj(const void *a,const void *b){if(*(int *)a<*(int *)b){return 1;}if(*(int *)a==*(int *)b){return 0;}return -1;}
int llsortfncsj(const void *a,const void *b){if(*(long long *)a>*(long long *)b){return 1;}if(*(long long *)a==*(long long *)b){return 0;}return -1;}
int llsortfnckj(const void *a,const void *b){if(*(long long *)a<*(long long *)b){return 1;}if(*(long long *)a==*(long long *)b){return 0;}return -1;}
int dbsortfncsj(const void *a,const void *b){if(*(double *)a>*(double *)b){return 1;}if(*(double *)a==*(double *)b){return 0;}return -1;}
int dbsortfnckj(const void *a,const void *b){if(*(double *)a<*(double *)b){return 1;}if(*(double *)a==*(double *)b){return 0;}return -1;}
int strsortfncsj(const void *a,const void *b){return strcmp((char *)a,(char *)b);}
int strsortfnckj(const void *a,const void *b){return strcmp((char *)b,(char *)a);}
int chsortfncsj(const void *a,const void *b){if(*(char *)a>*(char *)b){return 1;}if(*(char *)a==*(char *)b){return 0;}return -1;}
int chsortfnckj(const void *a,const void *b){if(*(char *)a<*(char *)b){return 1;}if(*(char *)a==*(char *)b){return 0;}return -1;}

void shuffledget(int x[],int n){
int i,b[524288],p,c;
for(i=0;i<n;i++){
b[i]=i;
}
for(i=n;i>=1;i--){
p=rand()%i;
c=b[i-1];b[i-1]=b[p];b[p]=c;
}
for(i=0;i<n;i++){
scanf("%d",&x[b[i]]);
}
}

int dx4[4]={1,-1,0,0};
int dy4[4]={0,0,1,-1};
int dx8[8]={-1,-1,-1,0,0,1,1,1};
int dy8[8]={-1,0,1,-1,1,-1,0,1};

int search(int x,int a[],int n){
int st=0,fi=n-1,te;
while(st<=fi){
te=(st+fi)/2;
if(a[te]<x){st=te+1;}else{fi=te-1;}
}
return st;
}

void prarr(int arr[],int n){
int i;
for(i=0;i<n;i++){
if(i){printf(" ");}
printf("%d",arr[i]);
}
printf("\n");
return;
}

void getperm(int a[],int n){
int i,p;
for(i=0;i<n;i++){
a[i]=i;
}
for(i=n-1;i>=1;i--){
p=rand()%(i+1);
swap(&a[p],&a[i]);
}
}

typedef struct{
int val;
int node;
}sd;

int sdsortfnc(const void *a,const void *b){
if(((sd*)a)->val < ((sd*)b)->val){return -1;}
if(((sd*)a)->val > ((sd*)b)->val){return 1;}
return 0;
}

void coordinate_comp(int a[],int n){
int i,c=0;
sd dat[524288];
for(i=0;i<n;i++){
dat[i].val=a[i];
dat[i].node=i;
}
qsort(dat,n,sizeof(dat[0]),sdsortfnc);
a[dat[0].node]=c;
for(i=1;i<n;i++){
if(dat[i-1].val!=dat[i].val){c++;}
a[dat[i].node]=c;
}
}

long long bdg(long long x){
long long r=0;
while(x>0){r++;x/=2;}
return r;
}

int main(void){
long long i,j,n,m,k,a[524288],b,c,h,w,r=0,l,t;
long long dg[32][2];
scanf("%lld",&t);
while(t>0){
t--;
r=0;
for(i=0;i<32;i++){
dg[i][0]=llinf;
dg[i][1]=-llinf;
}
scanf("%lld",&n);
for(i=0;i<n;i++){
scanf("%lld",&a[i]);
w=bdg(a[i]);
//printf("%lld\n",w);
dg[w][0]=llmin(a[i],dg[w][0]);
dg[w][1]=llmax(a[i],dg[w][1]);
}
for(i=1;i<31;i++){
for(j=1;j<31;j++){
if(dg[i][0]!=llinf){
if(dg[j][0]!=llinf){
r=llmax(llzt((dg[j][0]<<i)+dg[i][0],(dg[i][0]<<j)+dg[j][0]),r);
}
if(dg[j][1]!=-llinf){
r=llmax(llzt((dg[j][1]<<i)+dg[i][0],(dg[i][0]<<j)+dg[j][1]),r);
}
}
if(dg[i][1]!=-llinf){
if(dg[j][0]!=llinf){
r=llmax(llzt((dg[j][0]<<i)+dg[i][1],(dg[i][1]<<j)+dg[j][0]),r);
}
if(dg[j][1]!=-llinf){
r=llmax(llzt((dg[j][1]<<i)+dg[i][1],(dg[i][1]<<j)+dg[j][1]),r);
}
}
}
}
printf("%lld\n",r);
}
return 0;
}``````

## Binary Concatenation CodeChef Solution in JAVA

``````import java.io.*;
import java.util.*;
class Codechef {
public static long count(long nums1, long nums2) {
String binX=Long.toBinaryString(nums1);
String binY=Long.toBinaryString(nums2);
String binXplusY=binX+binY;
String binYplusX=binY+binX;
long XplusY=Long.parseLong(binXplusY,2);
long YplusX=Long.parseLong(binYplusX,2);

return XplusY-YplusX;
}
public static void main(String[] args) throws IOException
{
StdIn scn = new StdIn();
int t = scn.nextInt();
while (t--> 0) {
int n = scn.nextInt();
Arrays.sort(arr);
long max = Long.MIN_VALUE;
if(n<=100)
{
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
max=Math.max(max,Math.abs(count(arr[i],arr[j])));
}
}
}
else
{
for(int i=0;i<n;i++)
{
long temp=Math.abs(count(arr[n-1],arr[i]));
max=Math.max(temp,max);
}
}
System.out.println(max);
}
}
interface Input {
public String next();
public String nextLine();
public int nextInt();
public long nextLong();
public double nextDouble();
}
static class StdIn implements Input {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;

public StdIn() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
}

public StdIn(String filename) {
try{
din = new DataInputStream(new FileInputStream(filename));
} catch(Exception e) {
throw new RuntimeException();
}
buffer = new byte[BUFFER_SIZE];
}

public String next() {
int c;
StringBuilder s = new StringBuilder();
while (c != -1)
{
if (c == ' ' || c == '\n'||c=='\r')
break;
s.append((char)c);
}
return s.toString();
}

public String nextLine() {
int c;
StringBuilder s = new StringBuilder();
while (c != -1)
{
if (c == '\n'||c=='\r')
break;
s.append((char)c);
}
return s.toString();
}

public int nextInt() {
int ret = 0;
while (c <= ' ')
boolean neg = (c == '-');
if (neg)
do
{
ret = ret * 10 + c - '0';
}  while ((c = read()) >= '0' && c <= '9');

if (neg)
return -ret;
return ret;
}

int[] ar = new int[n];
for(int i=0; i<n; ++i)
ar[i]=nextInt();
return ar;
}

public long nextLong() {
long ret = 0;
while (c <= ' ')
boolean neg = (c == '-');
if (neg)
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}

long[] ar = new long[n];
for(int i=0; i<n; ++i)
ar[i]=nextLong();
return ar;
}

public double nextDouble() {
double ret = 0, div = 1;
while (c <= ' ')
boolean neg = (c == '-');
if (neg)

do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');

if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}

if (neg)
return -ret;
return ret;
}

private void fillBuffer() throws IOException {
buffer[0] = -1;
}

try{
fillBuffer();
return buffer[bufferPointer++];
} catch(IOException e) {
throw new RuntimeException();
}
}

public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
}``````

## Binary Concatenation CodeChef Solution in PYPY 3

``````#Smallest number closest to power of 2 on negative side largest and largest
#pow(2,len(o))-1 *n - (pow(2,len(n))-1)*o
#number closest to power of 2 on positive side
#
from collections import defaultdict
def binaryConcat(x,y):
xplusy=int(bin(x)[2:]+bin(y)[2:],2)
yplusx=int(bin(y)[2:]+bin(x)[2:],2)
return(abs(xplusy-yplusx))
def solution(arr):
dmax=defaultdict(int)
dmin=defaultdict(int)
for i in range(len(arr)):
blen=len(bin(arr[i]))-2
if(dmax[blen]==0 or dmax[blen]<arr[i]):
dmax[blen]=arr[i]
if(dmin[blen]==0 or dmin[blen]>arr[i]):
dmin[blen]=arr[i]
maxv=-float('inf')
for i in dmax.keys():
for j in dmin.keys():
bcat=binaryConcat(dmax[i],dmin[j])
if(bcat>maxv):
maxv=bcat
return maxv

#print(diffNegative(1))
t=int(input())
for _ in range(t):
n=input()
arr=list(map(int,input().split()))
print(solution(arr))
``````

## Binary Concatenation CodeChef Solution in PYTH

``````MX = []
v = 1
for i in range(30):
v = v*2
MX.append(v)
# endfor i
t = int(raw_input())
for i in range(t):
N = int(raw_input())
st = raw_input().split()
A = []
for x in st:
A.append(int(x))
# endfor x
A.sort()
A.append(MX[29])
H = [-1 for x in range(30)]
L = list(H)
p = 0
for m in range(30):
if A[p] < MX[m]:
L[m] = A[p]
p += 1
while A[p] < MX[m]:
p += 1
# endwhile
H[m] = A[p-1]
# endif
# endfor m
r = 0
for m in range(30):
if H[m] > -1:
for k in range(30):
if L[k] > -1:
n1 = H[m]*MX[k] + L[k]
n2 = L[k]*MX[m] + H[m]
v = abs(n1-n2)
if v > r:
r = v
# endif
# endif
# endfor k
# endif
# endfor m
print r
# endfor i
``````

## Binary Concatenation CodeChef Solution in C#

``````using System;
using System.Collections.Generic;
using System.IO;
using CompLib.Util;

public class Program
{
public void Solve()
{
var sc = new Scanner();
int t = sc.NextInt();
Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) {AutoFlush = false});
for (int i = 0; i < t; i++)
{
Q(sc);
}

Console.Out.Flush();
}

void Q(Scanner sc)
{
long n = sc.NextInt();
long[] a = sc.LongArray();
int[] length = new int[n];
for (int i = 0; i < n; i++)
{
long tmp = a[i];
for (; tmp > 0; tmp /= 2) length[i]++;
}

// lenのやつと操作したときの最大
var len = new long[31, 31];
for (int i = 0; i <= 30; i++)
{
for (int j = 0; j <= 30; j++)
{
len[i, j] = long.MinValue;
}
}

for (int x = 0; x < n; x++)
{
for (int j = 0; j <= 30; j++)
{
len[length[x], j] = Math.Max(len[length[x], j], (a[x] << j) - a[x]);
}
}

long ans = long.MinValue;
// a_iとlenのやつを操作した時
for (int y = 0; y < n; y++)
{
for (int i = 0; i <= 30; i++)
{
if (len[i, length[y]] == long.MinValue) continue;
long t = len[i, length[y]] + a[y] - (a[y] << i);
ans = Math.Max(ans, t);
}
}

Console.WriteLine(ans);
}

public static void Main(string[] args) => new Program().Solve();
}

namespace CompLib.Util
{
using System;
using System.Linq;

class Scanner
{
private string[] _line;
private int _index;
private const char Separator = ' ';

public Scanner()
{
_line = new string[0];
_index = 0;
}

public string Next()
{
while (_index >= _line.Length)
{
_index = 0;
}

return _line[_index++];
}

public int NextInt() => int.Parse(Next());
public long NextLong() => long.Parse(Next());
public double NextDouble() => double.Parse(Next());
public decimal NextDecimal() => decimal.Parse(Next());
public char NextChar() => Next()[0];
public char[] NextCharArray() => Next().ToCharArray();

public string[] Array()
{
_index = _line.Length;
return _line;
}

public int[] IntArray() => Array().Select(int.Parse).ToArray();
public long[] LongArray() => Array().Select(long.Parse).ToArray();
public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray();
}
}``````

## Binary Concatenation CodeChef Solution in GO

``````package main

import (
"bufio"
"fmt"
"os"
)

func readInt(bytes []byte, from int, val *int) int {
i := from
sign := 1
if bytes[i] == '-' {
sign = -1
i++
}
tmp := 0
for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
tmp = tmp*10 + int(bytes[i]-'0')
i++
}
*val = tmp * sign
return i
}

return
}

a, b = res[0], res[1]
return
}

a, b, c = res[0], res[1], res[2]
return
}

res := make([]int, n)
x := 0
for i := 0; i < n; i++ {
for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
x++
}
}
return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
i := from

var tmp uint64
for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
tmp = tmp*10 + uint64(bytes[i]-'0')
i++
}
*val = tmp

return i
}

func main() {

for tc > 0 {
tc--
fmt.Println(solve(n, A))
}
}

const N_INF = -(1 << 60)

func solve(n int, A []int) int64 {
dp := make([][][]int64, 2)
for i := 0; i < 2; i++ {
dp[i] = make([][]int64, 31)
for j := 0; j < 31; j++ {
dp[i][j] = make([]int64, 31)
for k := 0; k < 31; k++ {
dp[i][j][k] = N_INF
}
}
}

for i := 0; i < n; i++ {
x := A[i]
c := digitCount(x)
for j := 1; j < 31; j++ {
dp[0][c][j] = max(dp[0][c][j], (int64(x)<<uint64(j))-int64(x))
}
for j := 1; j < 31; j++ {
dp[1][j][c] = max(dp[1][j][c], int64(x)-(int64(x)<<uint64(j)))
}
}

var ans int64

for i := 1; i < 31; i++ {
for j := 1; j < 31; j++ {
ans = max(ans, dp[0][i][j]+dp[1][i][j])
}
}

return ans
}

func max(a, b int64) int64 {
if a >= b {
return a
}
return b
}

func digitCount(num int) int {
var res int
for num > 0 {
res++
num >>= 1
}
return res
}``````
##### Binary Concatenation CodeChef Solution Review:

In our experience, we suggest you solve this Binary Concatenation CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Binary Concatenation CodeChef Solution.

Find on CodeChef

##### Conclusion:

I hope this Binary Concatenation CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Programming Language in a business context; there are no prerequisites.

Keep Learning!