We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
defaOrB(k,a,b,c):#Converta,b,cto(0-left-padded)binaryarraysbin_len=4*max(map(len,[a,b,c]))a_bin,b_bin,c_bin=map(lambdax:[*map(int,f'{int(x,16):0>{bin_len}b}')],[a,b,c])# First pass: perform minimal bit changes to satisfy a'|b'==ca_new,b_new=[],[]forx,y,zinzip(a_bin,b_bin,c_bin):if(x|y)^z==1:if(x|y)==0:y=1#onlychangebitinb,tokeepa'minimalk-=1else:k-=x+yx,y=0,0a_new.append(x)b_new.append(y)# If after initial pass k<0, then no solutionifk<0:print(-1)else:# If k>0, can look for further minimizationsforiinrange(bin_len):ifk==0:breakelif(a_new[i],b_new[i])==(1,1):a_new[i]=0k-=1elif((a_new[i],b_new[i])==(1,0))and(k>1):a_new[i],b_new[i]=0,1k-=2a_new_hex,b_new_hex=map(lambdax:hex(int(''.join(map(str,x)),2))[2:].upper(),[a_new,b_new])print('\n'.join([a_new_hex,b_new_hex]))
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
A or B
You are viewing a single comment's thread. Return to all comments →
python3