📌  相关文章
📜  检查是否可以构造一个大小为 N 的数组,其总和为 S,XOR 值为 X

📅  最后修改于: 2022-05-13 01:56:05.676000             🧑  作者: Mango

检查是否可以构造一个大小为 N 的数组,其总和为 S,XOR 值为 X

给定三个数字N、SX ,任务是找出是否有可能构造一个长度为N的序列A ,其中每个A[i] >= 0表示1<=i<=N以及所有数字的总和一个序列等于S ,序列的按位异或等于X

例子:

方法:让我们考虑以下测试用例。

Case-1:N等于1时,很容易看出,当(S等于X)时,只返回“”,否则返回“”。

Case-2:N大于等于3时,使用公式(a + b) = (a xor b) + 2(a and b)这里可以看出(a + b) = S and (a xor b) = X所以方程变为S = X + 2(ab)。因此, (SX)应该是偶数,因为在右侧我们有2(ab)。所以,可以说是 S 是奇数,那么 X 是奇数,如果 S 是偶数,那么 X 是偶数,那么只有 (SX) 也是偶数,这可以通过(S%2 == X%2)S >= X否则 AB 变为负数,这是不可能的。

案例 3:对于案例N等于3,它类似于A + B + C = SA^B^C = X。使用属性A^A = 00^A = A => X + ( S – X)/2 + (S – X)/2 = X + (SX) => X + ( S – X)/2 + (S – X)/2 = S也是这样: X ^( ( S – X)/2 ^ (SX)/2 ) = X ^ 0 = X。因此,证明对于N == 3总会有这样的序列,我们可以返回“”。

案例 4:N == 2(S%2 == X%2)S >= X时,假设A + B == S(A^B) == X(A 和 B) == (SX)/2从上面讨论的等式。令C = AB仔细观察可以注意到,只有当 A 和 B 位在该位置为“1”时,C 的位才为“1”,否则为“1”。并且 X 是 A 的异或,B 只有在第 i位置有不同的位时 A 有 '0' 而 B 有 '1' 或正好相反:所以看看这个序列,将每个位分配到变量AB,C = ( S – X)/2。从 C 分配 A 和 B -> A = C, B = C

现在将X添加到AB以将所有 1 分配给A并将所有 0 分配给B所以当我们对两个数字进行异或时,添加到 A 中的“1”位将与我们添加到 B 中的“0”相反.有趣的部分是当 C 的设置位与 X 的一些设置位重合时,它不会给出 X 的所需 xor,现在, A = C + X,B = C。现在A+B = ( C + X) + C = S并且当 XOR AB 等于 X 时可以确定存在这样的对,当A + B == S(A^B) == X 时;

请按照以下步骤解决问题:

  • 如果S大于等于X,并且S%2等于X%2则执行以下步骤,否则返回No。
    • 如果n大于等于3,则返回Yes。
    • 如果n等于1,如果S等于X,则返回Yes ,否则返回No。
    • 如果n等于2,则将变量C初始化为(SX)/2 ,并将变量AB设置为C ,并将值X添加到变量A ,如果A^B等于X,则打印Yes ,否则打印No。

下面是上述方法的实现。

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find if any sequence is
// possible or not.
string findIfPossible(int N, int S, int X)
{
    if (S >= X and S % 2 == X % 2) {
 
        // Since, S is greater than equal to
        // X, and either both are odd or even
        // There always exists a sequence
        if (N >= 3) {
            return "Yes";
        }
        if (N == 1) {
 
            // Only one case possible is
            // S == X or NOT;
            if (S == X) {
                return "Yes";
            }
            else {
                return "No";
            }
        }
 
        // Considering the above conditions true,
        // check if XOR of S^(S-X) is X or not
        if (N == 2) {
 
            int C = (S - X) / 2;
            int A = C;
            int B = C;
            A = A + X;
            if (((A ^ B) == X)) {
                return "Yes";
            }
            else {
                return "No";
            }
        }
    }
    else {
        return "No";
    }
}
 
// Driver Code
int main()
{
 
    int N = 3, S = 10, X = 4;
 
    cout << findIfPossible(N, S, X);
 
    return 0;
}


C
// C program for the above approach
#include 
#include 
#include 
 
// Function to find if any sequence is
// possible or not.
char* findIfPossible(int N, int S, int X)
{
    if (S >= X && S % 2 == X % 2) {
 
        // Since, S is greater than equal to
        // X, and either both are odd or even
        // There always exists a sequence
        if (N >= 3) {
            return "Yes";
        }
        if (N == 1) {
 
            // Only one case possible is
            // S == X or NOT;
            if (S == X) {
                return "Yes";
            }
            else {
                return "No";
            }
        }
 
        // Considering the above conditions true,
        // check if XOR of S^(S-X) is X or not
        if (N == 2) {
 
            int C = (S - X) / 2;
            int A = C;
            int B = C;
            A = A + X;
            if (((A ^ B) == X)) {
                return "Yes";
            }
            else {
                return "No";
            }
        }
    }
    else {
        return "No";
    }
}
 
// Driver Code
int main()
{
 
    int N = 3, S = 10, X = 4;
    printf("%s\n", findIfPossible(N, S, X));
    return 0;
}
 
// This code is contributed by phalasi.


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to find if any sequence is
    // possible or not.
    static void findIfPossible(int N, int S, int X)
    {
        if ((S >= X) && (S % 2 == X % 2)) {
 
            // Since, S is greater than equal to
            // X, and either both are odd or even
            // There always exists a sequence
            if (N >= 3) {
                System.out.println("Yes");
            }
            if (N == 1) {
 
                // Only one case possible is
                // S == X or NOT;
                if (S == X) {
                    System.out.println("Yes");
                }
                else {
                    System.out.println("No");
                }
            }
 
            // Considering the above conditions true,
            // check if XOR of S^(S-X) is X or not
            if (N == 2) {
 
                int C = (S - X) / 2;
                int A = C;
                int B = C;
                A = A + X;
                if (((A ^ B) == X)) {
                    System.out.println("Yes");
                }
                else {
                    System.out.println("No");
                }
            }
        }
        else {
            System.out.println("No");
        }
    }
 
    // Driver code
    public static void main(String args[])
    {
        int N = 3, S = 10, X = 4;
 
        findIfPossible(N, S, X);
    }
}
 
// This code is contributed by code_hunt.


Python3
# Python program for the above approach
# Function to find if any sequence is
# possible or not.
 
 
def findIfPossible(N,  S,  X):
 
    if (S >= X and S % 2 == X % 2):
        # Since, S is greater than equal to
        # X, and either both are odd or even
        # There always exists a sequence
        if (N >= 3):
            return "Yes"
 
        if (N == 1):
 
            # Only one case possible is
            # S == X or NOT
            if (S == X):
                return "Yes"
            else:
                return "No"
 
        # Considering the above conditions true,
        # check if XOR of S^(S-X) is X or not
        if (N == 2):
            C = (S - X) // 2
            A = C
            B = C
            A = A + X
            if (((A ^ B) == X)):
                return "Yes"
            else:
                return "No"
    else:
        return "No"
 
 
# Driver Code
N = 3
S = 10
X = 4
 
print(findIfPossible(N, S, X))
 
# This code is contributed by shivanisinghss2110


C#
// C# program for the above approach
using System;
 
public class GFG {
 
    // Function to find if any sequence is
    // possible or not.
    static void findIfPossible(int N, int S, int X)
    {
        if ((S >= X) && (S % 2 == X % 2)) {
 
            // Since, S is greater than equal to
            // X, and either both are odd or even
            // There always exists a sequence
            if (N >= 3) {
                Console.WriteLine("Yes");
            }
            if (N == 1) {
 
                // Only one case possible is
                // S == X or NOT;
                if (S == X) {
                    Console.WriteLine("Yes");
                }
                else {
                    Console.WriteLine("No");
                }
            }
 
            // Considering the above conditions true,
            // check if XOR of S^(S-X) is X or not
            if (N == 2) {
 
                int C = (S - X) / 2;
                int A = C;
                int B = C;
                A = A + X;
                if (((A ^ B) == X)) {
                    Console.WriteLine("Yes");
                }
                else {
                    Console.WriteLine("No");
                }
            }
        }
        else {
            Console.WriteLine("No");
        }
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int N = 3, S = 10, X = 4;
 
        findIfPossible(N, S, X);
    }
}
 
// This code is contributed by Princi Singh


Javascript


输出
Yes

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