Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

minimum sum required to make xor of some integers to zero

Tags:

algorithm

xor

Here's a question which deals with algorithm and bitwise xor operation. We are given x1*x2*x3*....*xn=P, where star(*) operation represents XOR(bitwise) operation and x1 to xn are positive integers. P is also a positive integer. We need to find min(a1+a2+a3+.....an) such that this relation holds--> (x1+a1)*(x2+a2)*(x3+a3)*....*(xn+an)=0. '+' represents normal addition operation.

like image 484
halkujabra Avatar asked Sep 29 '12 19:09

halkujabra


People also ask

How do you get XOR of a number zero?

Naive Approach: Select an element and then find the XOR of the rest of the array. If that element became equals to XOR obtained then our XOR of the whole array should become zero.

When XOR value is minimum?

XOR is monotonic in the absolute difference between numbers. (If the numbers are identical, then the XOR is zero). If you ignore the possibility of negative numbers and write the numbers in binary, it becomes obvious. So the minimum value in a sorted list will always be between a particular adjacent pair.

How do you find the minimum XOR of two numbers?

Using Sorting In this approach, we will sort the input array and will find the minimum XOR value using a single loop. The sorting approach works because the XOR of two numbers that are nearer to each other has a lower XOR value as compared to the numbers that are distant. For example: 1 ^ 8 = 9 & 4 ^ 5 = 1.

How do you find 4 numbers whose XOR is 0?

Here it is given that all adjacent elements differ at only one position in their binary form thus there would result in only one set bit. Since we have only 64 possible positions, we can say that at least two pairs will have the same xor. Thus, xor of these 4 integers will be 0.


2 Answers

Restatement as a Bounded Integer Linear Programming Problem

The problem can be restated as the following bounded ILP (integer linear programming) problem.

Let x1,...,xN be as in the original problem, i.e. the goal is to minimize(a1+...+aN)
under the conditions (x1+a1) XOR ... (xN+aN) = 0, a1,...,aN >= 0.

The following is than an equivalent bounded ILP:
Find b[k,i] (0 <= k <= K, 1 <= i <= N) and s[k] (0 <= k <= K) such that
  (A) b[k,i] in {0, 1} for all i=1..n, k=0..K,
  (B) s[k] is an integer >= 0 for all k=0..K,
  (C) sum(b[k,i]*2^k, k=0..K) >= x[i] for all i = 1..N,
  (D) sum(b[k,i], i=1..N) = 2*s[k] for all k = 0..K
and minimize
  sum(b[k,i]*2^k, i=1..n, k=0..K).

From a solution of the bounded ILP the ai's are obtained by
  a[i] = sum(b[k,i]*2^k, k=0..(K-1)) - x[i]

b[k,i] is simply the k-th bit of the binary representation of (xi+ai) here (Conditions (A) and (C)). To make the overall XOR zero, an even number of b[k,i] must be 1 for every k. This is represented by conditions (B) and (D). (Note that the left-hand sum in (D) must be even if it's equal to 2*s[k] and s[k] is an integer).

K, i.e. the number of bits (plus one, actually) needed to represent all the (xi+ai) must to be determined beforehand. Picking a K such that all xi are < 2^K should suffice, i.e. the ai's are most one bit larger than the largest xi. But picking a larger K doesn't matter, the top bits will simply come out as zero. If K is picked to small, the ILP will have no solution.

Existance of a Solution

Ignoring the minimality condition, the problem can be restated as

Given x, y z with x <= y, find a and b such that (x+a) XOR (y+b) = z

Given an instance of your original problem, with N >= 2. Let x=x1, y=x2, z=(x3 XOR x4 XOR ... xn). If you find suitable a and b, set a1=a, a2=b, a3=...=an=0 to obtain a solution of the original problem.

The simplified problem is solved (again, ignoring minimality) by

  1. If (y XOR z) >= x then a: = ((y XOR z) - x), b := 0 is a solution (*)
  2. If (x XOR z) >= y then a := 0, b := ((x XOR z) - y) is a solution (*)
  3. Otherwise, pick an a such that (x+a) XOR z >= y. Such an a always exists, you can for example let a := 2^k with 2^k > max(x,y,z). Setting b := ((x+a) XOR z) - y) yields a solution (*)

(*) To see that those really are solutions, you just need to applied a bit of algebra. For example, in case (1), after substituting a and b, you get: (x+(y XOR z)-x) XOR (y+0). Which is identical to: (y XOR z) XOR y, and thus to: z. q.e.d. Case (2) works similarly. In case (3) you get: (x+a) XOR (y+((x+a) XOR z)-y) = (x+a) XOR ((x+a) XOR z) = z.

This shows that for N >= 2, a solution always exists.

Whether or not those solutions are minimal, I do not know. In case (3), it quite obviously depends on the choice of a, so at the very least you'd have to figure out the optimal choice for a. It might also be that the original problem allows for smaller solutions than the simplified problem. Anway, maybe this restatement helps someone to figure out the complete solution.

BTW, the fact that the original problems essentially leaves you total freedom of how to "spread out" the correction values ai over the xi makes me wonder if this isn't equivalent to some sort of knapsack problem. If it is, finding a minimal solution might very well be NP-hard.

Observations regarding minimality

Take the following problem

(1+a1) XOR (3+a2) XOR (6+a3) = 0

In binary, that is

(001b+a1) XOR (011b+a2) XOR (110b+a3) = 0

The residue for a1=a2=a3=0 is 001b XOR 011b XOR 110b = 100b. An obvious solution is thus a1=4,a2=0,a3=0. Or alternatively a1=0,a2=4,a3=0. This solution is not minimal though - the following solution is smaller

a1=1, a2=1, a3=0

It's also minimal, since all smaller solutions can only have one non-zero ai, and all of the terms (2 XOR 3 XOR 6), (1 XOR 4 XOR 6), (1 XOR 3 XOR 7) are non-zero.

This shows that no gree algorithm which works from the bottom (i.e. lowest bit) upwards can work, since such an algorithm would skip the first two bits, since their residue is zero initially.

It also shows that you cannot pick a certain non-zero bit k from the residue and attempt to zero it by adding 2^k to one of the xi's. You sometimes have to add it to multiple xi's to find the minimal solution.

This pushes me a bit further towards believing that find a minimal solution is a comparatively hard problem.

like image 87
fgp Avatar answered Sep 29 '22 14:09

fgp


Referring to my comment - with a question not answered yet:

  1. negative ai seem to be necessary: for the case N=1, a1=-x1 is the solution to the problem. I assume therefore that negative ai are allowed also in the general case.

  2. Then, for N=2, there is no solution ("min") at all, apart from the fact the there is a limit to what (negative) numbers can be represented in the computer:

For N=2 set: a1=x2+K and a2=x1+K. This yields:

(x1+x2+K) XOR (x2+x1+K) = 0, irrespective of K

The sum (a1 + a2) = (x1 + x2 + 2*K)

There is no minimum solution: we can make K ever more negative.

Likewise for N>2: For N even, make pair-wise "solutions" as for N=2 (with arbitrary K's); for N odd, single one out - treat it as for N=1 - and the remaining N-1 are handled as for even N.

In all cases you construct ZERO XOR ZERO XOR ZERO ... = ZERO, where ZERO is always a pair of the type (am = xm+1 + K; am+1 = xm + K), except when N is odd, where you have one other factor, i.e. of the type {am = -xm). Except for N=1, the solutions can become as large-negative as you like.

like image 42
Bert te Velde Avatar answered Sep 29 '22 16:09

Bert te Velde