Interview Question
Software DevelopersCountry: India
Interview Type: In-Person
perhaps your "interviewer was not quite satisfied with" your approach because it is obviously incorrect.. so for instance, if
A = [Y,Y,Y,]
B = [0,1,0]
then x (number of 1s) = 1 and A[i]*B[i] is same for every 'i'
and your algorithm will keep B as is
but flipping B[0] B[1] pair first and then the B[1] B[2] pair will give you
B = [1,1,1]
There is also a lot missing to get to a correct solution. For instance, what do we know about A? Is it sorted? If not, can it be sorted without maintaining its correspondence with B? Does it have negative integers?
Hint: sort A in desc order, and sort the corresponding B as well. Now you can flip the B bits from the beginning 2 by 2 to ideally get something like:
- dodo October 11, 20191 1 1 1 1 1 0 0 0 0 0 0
where the 1's correspond to positive A's and 0's correspond to negative A's. This is possible with at most 1 bit error where positives become negatives.