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

📅  最后修改于: 2021-09-03 04:09:29             🧑  作者: Mango

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

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

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

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

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

所以,现在让我们看看 a (给定 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,并且sum仍将为b,因为x为(ba)/2。

  • 案例4:剩下的唯一案例是检查数组是否存在。在这种情况下,只有两个条件需要检查:
    1. 如果按位异或大于总和。
    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)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live