📌  相关文章
📜  用给定的Sum和XOR构造最小的数组

📅  最后修改于: 2021-04-28 14:13:18             🧑  作者: Mango

给定两个正整数SX ,它们表示数组arr []的所有元素的总和与按位XOR。任务是找到数组arr []的元素。如果无法生成这样的数组,则打印-1。
例子:

方法:可以证明数组的最大长度最多为3。让我们考虑以下情况:

  • 情况1:如果给定的Sum和按位XOR相等且不为零,则该元素将是所需数组的唯一元素。
  • 情况2:对于总和与按位XOR不相等,最短的数组长度可以是2或3。
    如果给定的按位XOR和Sum分别为a和b,则使用以下两个XOR属性,最短数组可以为{a,(b – a)/ 2,(ba)/ 2}:
    • a Xor 0 = a
    • a Xor a = 0
  • 情况3:当数组的长度可以为2时。
    如果可能,我们在前一种情况下采用的数组可以减少为两个元素。

这里有用的一个重要公式是:

用上述公式代替sum和xor的值,我们得到了非常有用的关系。

因此,现在让我们看一下(给定的xor)和x((ba)/ 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
  • 注意: p和q表示数组中两个数字的所有相应32位。

重要的是要注意,如果a&x变为零,a + x = a ^ x ,这意味着数组将简化为{a + x,x},因为a + x = a ^ x 。根据上面的公式,它仍然可以导致总体XOR为a,并且和仍为b,因为x为(ba)/ 2。

  • 情况4:剩下的唯一情况是检查数组是否存在。在这种情况下,只有两个条件要检查为:
    1. 如果按位XOR大于和。
    2. 如果sum和xor具有不同的奇偶校验,即一个为偶数,另一个为奇数。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
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;
}


Java
// Java program implementation
// of the approach
import java.util.*;
import java.io.*;
 
class GFG{
     
// Function to find array
static void findArray(int sum, int xorr)
{
     
    // Array not possible
    if (xorr > sum  || sum % 2 != xorr % 2)
    {
        System.out.println("No Array Possible");
        return;
    }
 
    // Array possible with exactly 1
    // or no element
    if (sum == xorr)
    {
        if (sum == 0)
            System.out.println("Array is empty " +
                               "with size 0");
        else
            System.out.println("Array size is " + 1);
            System.out.println("Array is " + sum);
             
            return;
    }
    int mid = (sum - xorr) / 2;
 
    // Checking array with two
    // elements possible or not.
    if (xorr == 1 && mid == 1)
    {
        System.out.println("Array size is " + 3);
        System.out.println("Array is " + xorr +
                              " " + mid + " " + mid);
    }
    else
    {
        System.out.println("Array size is " + 2);
        System.out.println("Array is " + (xorr + mid) +
                                           " " + mid);
    }
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given sum and value
    // of Bitwise XOR
    int sum = 4, xorr = 2;
 
    // Function call
    findArray(sum, xorr);
}
}
 
// This code is contributed by sanjoy_62


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


C#
// C# program implementation
// of the approach
using System;
 
class GFG{
     
// Function to find array
static void findArray(int sum, int xorr)
{
 
    // Array not possible
    if (xorr > sum || sum % 2 != xorr % 2)
    {
        Console.WriteLine("No Array Possible");
        return;
    }
 
    // Array possible with exactly 1
    // or no element
    if (sum == xorr)
    {
        if (sum == 0)
            Console.WriteLine("Array is empty " +
                              "with size 0");
        else
            Console.WriteLine("Array size is " + 1);
            Console.WriteLine("Array is " + sum);
            return;
    }
    int mid = (sum - xorr) / 2;
 
    // Checking array with two
    // elements possible or not.
    if (xorr == 1 && mid == 1)
    {
        Console.WriteLine("Array size is " + 3);
        Console.WriteLine("Array is " + xorr +
                             " " + mid + " " + mid);
    }
    else
    {
        Console.WriteLine("Array size is " + 2);
        Console.WriteLine("Array is " + (xorr + mid) +
                                          " " + mid);
    }
}
 
// Driver code
public static void Main()
{
     
    // Given sum and value
    // of Bitwise XOR
    int sum = 4, xorr = 2;
 
    // Function call
    findArray(sum, xorr);
}
}
 
// This code is contributed by sanjoy_62


Javascript


输出:
Array size is 2
Array is 3 1

时间复杂度: O(1)
辅助空间: O(1)