给定一个整数数组arr []和一个整数K ,我们可以在任意数组元素和K之间执行任意次的按位或运算。任务是打印使数组的任何两个元素相等所需的最少数量的此类操作。如果执行上述操作后无法使数组的任何两个元素相等,则打印-1 。
例子:
Input: arr[] = {1, 9, 4, 3}, K = 3
Output: 1
We can OR a[0] with x, which makes it 3 which is equal to a[3]
Input : arr[] = {13, 26, 21, 15}, K = 13
Output : -1
方法:关键的发现是,如果是能够使所希望的阵列,则答案将是0,1或2。它永远不会超过2 。
Because, if (x | k) = y
then, no matter how many times you perform (y | k)
it’ll always give y as the result.
- 如果数组中已经有相等的元素,则答案将为0 。
- 为了使答案为1 ,我们将创建一个新数组b [] ,其中包含b [i] =(a [i] | K) ,
现在,对于每个a [i],我们将检查是否存在任何索引j ,使得i!= j且a [i] = b [j] 。
如果是,那么答案将是1 。 - 对于答案为2 ,我们将检查新数组b []中的索引i ,
如果存在任何索引j ,使得i!= j且b [i] = b [j] 。
如果是,那么答案将是2 。 - 如果不满足上述任何条件,则答案将为-1 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the count of
// minimum operations required
int minOperations(int a[], int n, int K)
{
unordered_map map;
for (int i = 0; i < n; i++) {
// Check if the initial array
// already contains an equal pair
if (map[a[i]])
return 0;
map[a[i]] = true;
}
// Create new array with OR operations
int b[n];
for (int i = 0; i < n; i++)
b[i] = a[i] | K;
// Clear the map
map.clear();
// Check if the solution
// is a single operation
for (int i = 0; i < n; i++) {
// If Bitwise OR operation between
// 'k' and a[i] gives
// a number other than a[i]
if (a[i] != b[i])
map[b[i]] = true;
}
// Check if any of the a[i]
// gets equal to any other element
// of the array after the operation
for (int i = 0; i < n; i++)
// Single operation
// will be enough
if (map[a[i]])
return 1;
// Clear the map
map.clear();
// Check if the solution
// is two operations
for (int i = 0; i < n; i++) {
// Check if the array 'b'
// contains duplicates
if (map[b[i]])
return 2;
map[b[i]] = true;
}
// Otherwise it is impossible to
// create such an array with
// Bitwise OR operations
return -1;
}
// Driver code
int main()
{
int K = 3;
int a[] = { 1, 9, 4, 3 };
int n = sizeof(a) / sizeof(a[0]);
// Function call to compute the result
cout << minOperations(a, n, K);
return 0;
}
Java
// Java implementation of the approach
import java.util.HashMap;
class GFG
{
// Function to return the count of
// minimum operations required
public static int minOperations(int[] a, int n, int K)
{
HashMap map = new HashMap<>();
for (int i = 0; i < n; i++)
{
// Check if the initial array
// already contains an equal pair
if (map.containsKey(a[i]))
return 0;
map.put(a[i], true);
}
// Create new array with OR operations
int[] b = new int[n];
for (int i = 0; i < n; i++)
b[i] = a[i] | K;
// Clear the map
map.clear();
// Check if the solution
// is a single operation
for (int i = 0; i < n; i++)
{
// If Bitwise OR operation between
// 'k' and a[i] gives
// a number other than a[i]
if (a[i] != b[i])
map.put(b[i], true);
}
// Check if any of the a[i]
// gets equal to any other element
// of the array after the operation
for (int i = 0; i < n; i++)
{
// Single operation
// will be enough
if (map.containsKey(a[i]))
return 1;
}
// Clear the map
map.clear();
// Check if the solution
// is two operations
for (int i = 0; i < n; i++)
{
// Check if the array 'b'
// contains duplicates
if (map.containsKey(b[i]))
return 2;
map.put(b[i], true);
}
// Otherwise it is impossible to
// create such an array with
// Bitwise OR operations
return -1;
}
// Driver code
public static void main(String[] args)
{
int K = 3;
int[] a = { 1, 9, 4, 3 };
int n = a.length;
System.out.println(minOperations(a, n, K));
}
}
// This code is contributed by
// sanjeev2552
Python3
# Python3 implementation of the approach
# Function to return the count of
# minimum operations required
def minOperations(a, n, K) :
map = dict.fromkeys(a,0) ;
for i in range(n) :
# Check if the initial array
# already contains an equal pair
if (map[a[i]]) :
return 0;
map[a[i]] = True;
# Create new array with OR operations
b = [0]*n;
for i in range(n) :
b[i] = a[i] | K;
# Clear the map
map.clear();
# Check if the solution
# is a single operation
for i in range(n) :
# If Bitwise OR operation between
# 'k' and a[i] gives
# a number other than a[i]
if (a[i] != b[i]) :
map[b[i]] = True;
# Check if any of the a[i]
# gets equal to any other element
# of the array after the operation
for i in range(n) :
# Single operation
# will be enough
if a[i] not in map :
pass
elif (map[a[i]]) :
return 1;
# Clear the map
map.clear();
# Check if the solution
# is two operations
for i in range(n) :
# Check if the array 'b'
# contains duplicates
if (map[b[i]]) :
return 2;
map[b[i]] = true;
# Otherwise it is impossible to
# create such an array with
# Bitwise OR operations
return -1;
# Driver code
if __name__ == "__main__" :
K = 3;
a = [ 1, 9, 4, 3 ];
n = len(a);
# Function call to compute the result
print(minOperations(a, n, K));
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to return the count of
// minimum operations required
public static int minOperations(int[] a,
int n, int K)
{
Dictionary map = new Dictionary();
for (int i = 0; i < n; i++)
{
// Check if the initial array
// already contains an equal pair
if (map.ContainsKey(a[i]))
return 0;
map.Add(a[i], true);
}
// Create new array with OR operations
int[] b = new int[n];
for (int i = 0; i < n; i++)
b[i] = a[i] | K;
// Clear the map
map.Clear();
// Check if the solution
// is a single operation
for (int i = 0; i < n; i++)
{
// If Bitwise OR operation between
// 'k' and a[i] gives
// a number other than a[i]
if (a[i] != b[i])
map.Add(b[i], true);
}
// Check if any of the a[i]
// gets equal to any other element
// of the array after the operation
for (int i = 0; i < n; i++)
{
// Single operation
// will be enough
if (map.ContainsKey(a[i]))
return 1;
}
// Clear the map
map.Clear();
// Check if the solution
// is two operations
for (int i = 0; i < n; i++)
{
// Check if the array 'b'
// contains duplicates
if (map.ContainsKey(b[i]))
return 2;
map.Add(b[i], true);
}
// Otherwise it is impossible to
// create such an array with
// Bitwise OR operations
return -1;
}
// Driver code
public static void Main(String[] args)
{
int K = 3;
int[] a = { 1, 9, 4, 3 };
int n = a.Length;
Console.WriteLine(minOperations(a, n, K));
}
}
// This code is contributed by 29AjayKumar
输出:
1