给定以a,b,c表示的数组N个元素的按位AND , OR和XOR 。任务是找到数组的元素。如果不存在这样的数组,则打印“ -1”。
例子:
Input: N = 3, a = 4, b = 6, c = 6.
Output: {4, 4, 6}
Explanation:
Bitwise AND of array = 4 & 4 & 6 = 4
Bitwise OR of array = 4 | 4 | 6 = 6
Bitwise XOR of array = 4 ^ 4 ^ 6 = 6
Input: N = 2, a = 4, b = 6, c = 6.
Output: -1
方法:
- 对于按位与,如果在中设置了第i个位,则在数组中每个元素都必须设置第i个位,因为即使一个元素的第i个位为0,那么数组的按位与将导致第i个位为0 。
- 其次,如果第a位中未设置第i位,则需要同时处理OR和XOR值。如果在b中设置了第i个位,则至少一个元素必须设置有第i个位。因此,在数组唯一的第一个元素中设置第i位。
- 现在,如果第i位在B设置则第i位必须在C检查。如果在c中设置了该位,那么就没有问题,因为第一个元素的第i个位已被设置,因此1 ^ 0 =1。如果在c中没有设置该位,则将第二个元素的第i个位设置为1 。现在,在b中将没有任何影响,对于c, 1 ^ 1将为0 。
- 然后,只需计算数组的按位“与”,“或”和“异或”,即可检查其是否相等。如果结果不相等,则该数组是不可能的,否则给定数组就是答案。
下面是该方法的实现:
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现场课程》和《 Geeks现场课程美国》。