Given two positive integers **S** and **X** which represents the sum and Bitwise XOR of all the elements of an array **arr[]**. The task is to find the elements of the array **arr[]**. If no such array can be generated, print -1.

**Examples:**

Input:Sum = 4, Xor = 2

Output:{3, 1}

Explanation:

Sum of 1 and 3 is 4.

Bitwise XOR of 1 and 3 is 2.

Input:Sum = 5, Xor = 8

Output:-1

Explanation:

There is no such array exists.

**Approach:** It can be proven that the maximum length of the array will be at most 3. Let us consider the following cases:

**Case 1:**If the given Sum and Bitwise XOR are equal and non-zero, then, that element will be the only element of the required array.**Case 2:**For unequal Sum and Bitwise XOR, the shortest array length can be either 2 or 3.

If the given Bitwise XOR and Sum is a and b respectively, then the shortest array can be {a, (b – a)/2, (b-a)/2 } by using the following two properties of XOR:- a Xor 0 = a
- a Xor a = 0

a + x + x = b

2*x = b-a

x = (b-a)/2Hence, if we take x as (b-a)/2 then xor will be a, and the sum will also be b.

**Case 3:**When length of array can be 2.

The array we took in the previous case can be reduced to two elements if it is possible.One important formula helpful here is:-

**p + q = (p ^ q) + 2*(p & q)**Substituting values of sum and xor in the above formula we get a very helpful relation.

b = a + 2*(p & q)

(p & q) = (b-a)/2

From previous case,

x = (b-a)/2 = (p & q)So, now let’s see some relation between a (given xor) and x ((b-a)/2).

**p q a=(p^q) x=(p&q) a&x**0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1 1 0 1 0**Note:**p and q represents all corresponding 32 bits of two numbers in array.It is important to note that if

**a & x**becomes**zero**then**a + x = a ^ x**which means the array will be reduced to**{a+x, x}**because**a+x = a^x**. From the above formula, which can still lead to overall XOR as a, and sum will still be b as x is (b-a)/2.**Case 4:**The only case left is to check whether array exists or not. In this case, there are only two condition to check as:- If Bitwise XOR is greater than the sum.
- If sum and xor have different parities i.e., one is even and other is odd.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find array ` `void` `findArray(` `int` `sum, ` `int` `xorr) ` `{ ` ` ` `// array not possible ` ` ` `if` `(xorr > sum ` ` ` `|| sum % 2 != xorr % 2) { ` ` ` `cout << ` `"No Array Possible\n"` `; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Array possible with exactly 1 ` ` ` `// or no element ` ` ` `if` `(sum == xorr) { ` ` ` `if` `(sum == 0) ` ` ` `cout << ` `"Array is empty"` ` ` `<< ` `" with size 0\n"` `; ` ` ` `else` ` ` `cout << ` `"Array size is "` ` ` `<< 1 ` ` ` `<< ` `"\n Array is "` ` ` `<< sum << ` `"\n"` `; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `int` `mid = (sum - xorr) / 2; ` ` ` ` ` `// Checking array with two ` ` ` `// elements possible or not. ` ` ` `if` `(xorr & mid == 1) { ` ` ` `cout << ` `"Array size is "` ` ` `<< 3 << ` `"\n"` `; ` ` ` ` ` `cout << ` `"Array is "` ` ` `<< xorr << ` `" "` ` ` `<< mid << ` `" "` ` ` `<< mid << ` `"\n"` `; ` ` ` `} ` ` ` `else` `{ ` ` ` ` ` `cout << ` `"Array size is "` ` ` `<< 2 << ` `"\n"` `; ` ` ` ` ` `cout << ` `"Array is "` ` ` `<< (xorr + mid) ` ` ` `<< ` `" "` ` ` `<< mid << ` `"\n"` `; ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Given sum and value ` ` ` `// of Bitwise XOR ` ` ` `int` `sum = 4, xorr = 2; ` ` ` ` ` `// Function Call ` ` ` `findArray(sum, xorr); ` ` ` `cout << ` `"\n"` `; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach ` ` ` `# Function to find array ` `def` `findArray(_sum, xorr): ` ` ` ` ` `# Array not possible ` ` ` `if` `(xorr > _sum ` `or` ` ` `_sum ` `%` `2` `!` `=` `xorr ` `%` `2` `): ` ` ` `print` `(` `"No Array Possible"` `) ` ` ` `return` ` ` ` ` `# Array possible with exactly 1 ` ` ` `# or no element ` ` ` `if` `(_sum ` `=` `=` `xorr): ` ` ` `if` `(_sum ` `=` `=` `0` `): ` ` ` `print` `(` `"Array is empty"` `, ` ` ` `" with size 0"` `) ` ` ` ` ` `else` `: ` ` ` `print` `(` `"Array size is"` `, ` `1` `) ` ` ` `print` `(` `"Array is"` `, _sum) ` ` ` `return` ` ` ` ` `mid ` `=` `(_sum ` `-` `xorr) ` `/` `/` `2` ` ` ` ` `# Checking array with two ` ` ` `# elements possible or not. ` ` ` `if` `(xorr & mid ` `=` `=` `1` `): ` ` ` `print` `(` `"Array size is"` `, ` `3` `) ` ` ` `print` `(` `"Array is"` `, xorr, mid, mid) ` ` ` ` ` `else` `: ` ` ` `print` `(` `"Array size is"` `, ` `2` `) ` ` ` ` ` `print` `(` `"Array is"` `,(xorr ` `+` `mid), mid) ` ` ` `# Driver Code ` ` ` `# Given sum and value ` `# of Bitwise XOR ` `_sum ` `=` `4` `xorr ` `=` `2` ` ` `# Function Call ` `findArray(_sum, xorr) ` ` ` `# This code is contributed by divyamohan123 ` |

*chevron_right*

*filter_none*

**Output:**

Array size is 2 Array is 3 1

**Time Complexity:** *O(1)*

**Auxiliary Space:** *O(1)*

## Recommended Posts:

- Construct an Array of Strings having Longest Common Prefix specified by the given Array
- Construct an array from GCDs of consecutive elements in given array
- Construct an array from XOR of all elements of array except element at same index
- Find the smallest and second smallest elements in an array
- Maximum sum of smallest and second smallest in an array
- Construct the Array using given bitwise AND, OR and XOR
- Construct sum-array with sum of elements in given range
- Construct an array from its pair-product
- Construct an Array such that cube sum of all element is a perfect square
- Construct array having X subsequences with maximum difference smaller than d
- Construct a frequency array of digits of the values obtained from x^1, x^2, ........, x^n
- Construct an Array of size N whose sum of cube of all elements is a perfect square
- Count of suffix increment/decrement operations to construct a given array
- Rearrange an array in order - smallest, largest, 2nd smallest, 2nd largest, ..
- Construct Binary Tree from given Parent Array representation | Iterative Approach
- Construct a distinct elements array with given size, sum and element upper bound
- Kth smallest element in the array using constant space when array can't be modified
- Sort Array such that smallest is at 0th index and next smallest it at last index and so on
- Smallest Pair Sum in an array
- Construct an Array of size N in which sum of odd elements is equal to sum of even elements

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.