📌  相关文章
📜  使用给定的按位 AND、OR 和 XOR 构造数组

📅  最后修改于: 2021-09-07 02:22:50             🧑  作者: Mango

给定由 a、b、c 表示的数组的N 个元素的按位ANDORXOR 。任务是找到数组的元素。如果不存在这样的数组,则打印“-1”。
例子:

方法:

  1. 对于按位与,如果在 a 中设置了i 位,则在数组中每个元素都必须设置i 位,因为即使一个元素的i 位0,则数组的按位与将导致i 位为 0 .
  2. 其次,如果 a 中没有设置i 位,则需要同时处理 OR 和 XOR 值。如果在 b 中设置了i 位,则至少一个元素必须设置i 位。因此,在数组的唯一第一个元素中设置i 位。
  3. 现在,如果第iB设置则第i必须在C检查。如果该位在 c 中设置,则没有问题,因为第一个元素的i 位已经设置为 1 ^ 0 = 1。如果该位未在 c 中设置,则设置第二个元素的i 位。现在, b 不会有任何影响,对于 c, 1 ^ 1 将为 0
  4. 然后,只需计算数组的按位 AND、OR 和 XOR 以检查它是否相等。如果结果不相等,则数组是不可能的,否则给出的数组就是答案。

下面是该方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the array
void findArray(int n, int a,
               int b, int c)
{
    int arr[n + 1] = {};
 
    // Loop through all bits in number
    for (int bit = 30; bit >= 0; bit--) {
 
        // If bit is set in AND
        // then set it in every element
        // of the array
        int set = a & (1 << bit);
        if (set) {
            for (int i = 0; i < n; i++)
                arr[i] |= set;
        }
 
        // If bit is not set in AND
        else {
 
            // But set in b(OR)
            if (b & (1 << bit)) {
 
                // Set bit position
                // in first element
                arr[0] |= (1 << bit);
 
                // If bit is not set in c
                // then set it in second
                // element to keep xor as
                // zero for bit position
                if (!(c & (1 << bit))) {
                    arr[1] |= (1 << bit);
                }
            }
        }
    }
 
    int aa = INT_MAX, bb = 0, cc = 0;
 
    // Calculate AND, OR
    // and XOR of array
    for (int i = 0; i < n; i++) {
        aa &= arr[i];
        bb |= arr[i];
        cc ^= arr[i];
    }
 
    // Check if values are equal or not
    if (a == aa && b == bb && c == cc) {
        for (int i = 0; i < n; i++)
            cout << arr[i] << " ";
    }
 
    // If not, then array
    // is not possible
    else
        cout << "-1";
}
 
// Driver Code
int main()
{
    // Given Bitwise AND, OR, and XOR
    int n = 3, a = 4, b = 6, c = 6;
 
    // Function Call
    findArray(n, a, b, c);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
 
// Function to find the array
static void findArray(int n, int a,
                      int b, int c)
{
    int arr[] = new int[n + 1];
 
    // Loop through all bits in number
    for(int bit = 30; bit >= 0; bit--)
    {
         
       // If bit is set in AND
       // then set it in every element
       // of the array
       int set = a & (1 << bit);
       if (set != 0)
       {
           for(int i = 0; i < n; i++)
              arr[i] |= set;
       }
        
       // If bit is not set in AND
       else
       {
            
           // But set in b(OR)
           if ((b & (1 << bit)) != 0)
           {
                
               // Set bit position
               // in first element
               arr[0] |= (1 << bit);
                
               // If bit is not set in c
               // then set it in second
               // element to keep xor as
               // zero for bit position
               if ((c & (1 << bit)) == 0)
               {
                   arr[1] |= (1 << bit);
               }
           }
       }
    }
    int aa = Integer.MAX_VALUE, bb = 0, cc = 0;
     
    // Calculate AND, OR
    // and XOR of array
    for(int i = 0; i < n; i++)
    {
       aa &= arr[i];
       bb |= arr[i];
       cc ^= arr[i];
    }
 
    // Check if values are equal or not
    if (a == aa && b == bb && c == cc)
    {
        for(int i = 0; i < n; i++)
           System.out.print(arr[i] + " ");
    }
 
    // If not, then array
    // is not possible
    else
        System.out.println("-1");
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given Bitwise AND, OR, and XOR
    int n = 3, a = 4, b = 6, c = 6;
 
    // Function Call
    findArray(n, a, b, c);
}
}
 
// This code is contributed by Pratima Pandey


Python3
# Python3 program for
# the above approach
import sys
 
# Function to find the array
def findArray(n, a, b, c):
 
    arr = [0] * (n + 1)
 
    # Loop through all bits in number
    for bit in range (30, -1, -1):
 
        # If bit is set in AND
        # then set it in every element
        # of the array
        set = a & (1 << bit)
        if (set):
            for i in range (n):
                arr[i] |= set
 
        # If bit is not set in AND
        else :
 
            # But set in b(OR)
            if (b & (1 << bit)):
 
                # Set bit position
                # in first element
                arr[0] |= (1 << bit)
 
                # If bit is not set in c
                # then set it in second
                # element to keep xor as
                # zero for bit position
                if (not (c & (1 << bit))):
                    arr[1] |= (1 << bit)
 
    aa = sys.maxsize
    bb = 0
    cc = 0
 
    # Calculate AND, OR
    # and XOR of array
    for i in range (n):
        aa &= arr[i]
        bb |= arr[i]
        cc ^= arr[i]
 
    # Check if values are equal or not
    if (a == aa and b == bb and c == cc):
        for i in range (n):
            print (arr[i], end = " ")
    
    # If not, then array
    # is not possible
    else:
        print ("-1")
 
# Driver Code
if __name__ =="__main__":
   
    # Given Bitwise AND, OR, and XOR
    n = 3
    a = 4
    b = 6
    c = 6
 
    # Function Call
    findArray(n, a, b, c)
     
# This code is contributed by Chitranayal


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the array
static void findArray(int n, int a,
                      int b, int c)
{
    int []arr = new int[n + 1];
 
    // Loop through all bits in number
    for(int bit = 30; bit >= 0; bit--)
    {
        
       // If bit is set in AND
       // then set it in every element
       // of the array
       int set = a & (1 << bit);
       if (set != 0)
       {
           for(int i = 0; i < n; i++)
              arr[i] |= set;
       }
        
       // If bit is not set in AND
       else
       {
            
           // But set in b(OR)
           if ((b & (1 << bit)) != 0)
           {
                
               // Set bit position
               // in first element
               arr[0] |= (1 << bit);
                
               // If bit is not set in c
               // then set it in second
               // element to keep xor as
               // zero for bit position
               if ((c & (1 << bit)) == 0)
               {
                   arr[1] |= (1 << bit);
               }
           }
       }
    }
    int aa = int.MaxValue, bb = 0, cc = 0;
     
    // Calculate AND, OR
    // and XOR of array
    for(int i = 0; i < n; i++)
    {
       aa &= arr[i];
       bb |= arr[i];
       cc ^= arr[i];
    }
 
    // Check if values are equal or not
    if (a == aa && b == bb && c == cc)
    {
        for(int i = 0; i < n; i++)
           Console.Write(arr[i] + " ");
    }
 
    // If not, then array
    // is not possible
    else
        Console.WriteLine("-1");
}
 
// Driver code
public static void Main(String[] args)
{
     
    // Given Bitwise AND, OR, and XOR
    int n = 3, a = 4, b = 6, c = 6;
 
    // Function Call
    findArray(n, a, b, c);
}
}
 
// This code is contributed by gauravrajput1


输出:
6 4 4


时间复杂度: O(31*N)

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