给定大小为N的数组arr [] ,任务是查找给定数组中所有可能的无序对的按位与。
例子:
Input: arr[] = {1, 5, 3, 7}
Output: 1
Explanation:
All possible unordered pairs are (1, 5), (1, 3), (1, 7), (5, 3), (5, 7), (3, 7).
Bitwise AND of all possible pairs = ( 1 & 5 ) & ( 1 & 3 ) & ( 1 & 7 ) & ( 5 & 3 ) & ( 5 & 7 ) & ( 3 & 7 )
= 1 & 1 & 1 & 1 & 5 & 3
= 1
Therefore, the required output is 1.
Input: arr[] = {4, 5, 12, 15}
Output: 4
天真的方法:想法是遍历数组并生成给定数组的所有可能的对。最后,打印给定数组的这些对中存在的每个元素的按位与。请按照以下步骤解决问题:
- 初始化一个变量,例如totalAND ,以存储这些对中每个元素的按位与。
- 遍历数组,并从给定数组生成所有可能的对( arr [i],arr [j] )。
- 对于每对( arr [i],arr [j] ),更新totalAND =(totalAND&arr [i]&arr [j])的值。
- 最后,打印totalAND的值。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to calulate bitwise AND
// of all pairs from the given array
int TotalAndPair(int arr[], int N)
{
// Stores bitwise AND
// of all possible pairs
int totalAND = (1 << 30) - 1;
// Generate all possible pairs
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N;
j++) {
// Calculate bitwise AND
// of each pair
totalAND &= arr[i]
& arr[j];
}
}
return totalAND;
}
// Driver Code
int main()
{
int arr[] = { 4, 5, 12, 15 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << TotalAndPair(arr, N);
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function to calulate bitwise AND
// of all pairs from the given array
static int TotalAndPair(int arr[], int N)
{
// Stores bitwise AND
// of all possible pairs
int totalAND = (1 << 30) - 1;
// Generate all possible pairs
for (int i = 0; i < N; i++)
{
for (int j = i + 1; j < N;
j++)
{
// Calculate bitwise AND
// of each pair
totalAND &= arr[i]
& arr[j];
}
}
return totalAND;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 4, 5, 12, 15 };
int N = arr.length;
System.out.print(TotalAndPair(arr, N));
}
}
// This code is contributed by shikhasingrajput
Python3
# Python3 program to implement
# the above approach
# Function to calulate bitwise AND
# of all pairs from the given array
def TotalAndPair(arr, N):
# Stores bitwise AND
# of all possible pairs
totalAND = (1 << 30) - 1
# Generate all possible pairs
for i in range(N):
for j in range(i + 1, N):
# Calculate bitwise AND
# of each pair
totalAND &= (arr[i] & arr[j])
return totalAND
# Driver Code
if __name__ == '__main__':
arr=[4, 5, 12, 15]
N = len(arr)
print(TotalAndPair(arr, N))
# This code is contributed by mohit kumar 29
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to calulate bitwise AND
// of all pairs from the given array
static int TotalAndPair(int[] arr, int N)
{
// Stores bitwise AND
// of all possible pairs
int totalAND = (1 << 30) - 1;
// Generate all possible pairs
for(int i = 0; i < N; i++)
{
for(int j = i + 1; j < N; j++)
{
// Calculate bitwise AND
// of each pair
totalAND &= arr[i] & arr[j];
}
}
return totalAND;
}
// Driver Code
public static void Main()
{
int[] arr = { 4, 5, 12, 15 };
int N = arr.Length;
Console.Write(TotalAndPair(arr, N));
}
}
// This code is contributed by sanjoy_62
Javascript
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to calulate bitwise AND
// of all pairs from the given array
int TotalAndPair(int arr[], int N)
{
// Stores bitwise AND
// of all possible pairs
int totalAND = (1 << 30) - 1;
// Iterate over the array arr[]
for (int i = 0; i < N; i++) {
// Calculate bitwise AND
// of each array element
totalAND &= arr[i];
}
return totalAND;
}
// Driver Code
int main()
{
int arr[] = { 4, 5, 12, 15 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << TotalAndPair(arr, N);
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function to calulate bitwise AND
// of all pairs from the given array
static int TotalAndPair(int arr[], int N)
{
// Stores bitwise AND
// of all possible pairs
int totalAND = (1 << 30) - 1;
// Iterate over the array arr[]
for (int i = 0; i < N; i++) {
// Calculate bitwise AND
// of each array element
totalAND &= arr[i];
}
return totalAND;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 4, 5, 12, 15 };
int N = arr.length;
System.out.print(TotalAndPair(arr, N));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python program to implement
# the above approach
# Function to calulate bitwise AND
# of all pairs from the given array
def TotalAndPair(arr, N):
# Stores bitwise AND
# of all possible pairs
totalAND = (1 << 30) - 1;
# Iterate over the array arr
for i in range(N):
# Calculate bitwise AND
# of each array element
totalAND &= arr[i];
return totalAND;
# Driver Code
if __name__ == '__main__':
arr = [4, 5, 12, 15];
N = len(arr);
print(TotalAndPair(arr, N));
# This code is contributed by 29AjayKumar
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to calulate bitwise AND
// of all pairs from the given array
static int TotalAndPair(int []arr, int N)
{
// Stores bitwise AND
// of all possible pairs
int totalAND = (1 << 30) - 1;
// Iterate over the array arr[]
for(int i = 0; i < N; i++)
{
// Calculate bitwise AND
// of each array element
totalAND &= arr[i];
}
return totalAND;
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 4, 5, 12, 15 };
int N = arr.Length;
Console.Write(TotalAndPair(arr, N));
}
}
// This code is contributed by AnkThon
Javascript
4
时间复杂度: O(N 2 )
辅助空间: O(1)
高效的方法:可以基于以下观察来优化上述方法:
- 考虑到由4个元素组成的数组,所需的按位AND如下所示:
(arr[0] & arr[1]) & (arr[0] & arr[2]) & (arr[0] & arr[3]) & (arr[1] & arr[2]) & (arr[1] & arr[3]) & (arr[2] & arr[3])
- 由于按位AND遵循Associative属性,因此上述表达式可以重新安排为:
(arr[0] & arr[0] & arr[0]) & (arr[1] & arr[1] & arr[1]) & (arr[2] & arr[2] & arr[2]) & (arr[3] & arr[3] & arr[3])
- 可以观察到,每个数组元素在所有可能的对中精确地(N – 1)次出现。
- 基于按位与运算符的X&X = X属性,可以将上述表达式重新排列为arr [0]&arr [1]&arr [2]&arr [3] ,它等于所有原始数组的元素。
请按照以下步骤解决问题:
- 初始化变量totalAND以存储结果。
- 遍历给定的数组。
- 通过更新totalAND = totalAND&arr [i]来计算所有无序对的按位与。
下面是上述方法的实现
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to calulate bitwise AND
// of all pairs from the given array
int TotalAndPair(int arr[], int N)
{
// Stores bitwise AND
// of all possible pairs
int totalAND = (1 << 30) - 1;
// Iterate over the array arr[]
for (int i = 0; i < N; i++) {
// Calculate bitwise AND
// of each array element
totalAND &= arr[i];
}
return totalAND;
}
// Driver Code
int main()
{
int arr[] = { 4, 5, 12, 15 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << TotalAndPair(arr, N);
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function to calulate bitwise AND
// of all pairs from the given array
static int TotalAndPair(int arr[], int N)
{
// Stores bitwise AND
// of all possible pairs
int totalAND = (1 << 30) - 1;
// Iterate over the array arr[]
for (int i = 0; i < N; i++) {
// Calculate bitwise AND
// of each array element
totalAND &= arr[i];
}
return totalAND;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 4, 5, 12, 15 };
int N = arr.length;
System.out.print(TotalAndPair(arr, N));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python program to implement
# the above approach
# Function to calulate bitwise AND
# of all pairs from the given array
def TotalAndPair(arr, N):
# Stores bitwise AND
# of all possible pairs
totalAND = (1 << 30) - 1;
# Iterate over the array arr
for i in range(N):
# Calculate bitwise AND
# of each array element
totalAND &= arr[i];
return totalAND;
# Driver Code
if __name__ == '__main__':
arr = [4, 5, 12, 15];
N = len(arr);
print(TotalAndPair(arr, N));
# This code is contributed by 29AjayKumar
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to calulate bitwise AND
// of all pairs from the given array
static int TotalAndPair(int []arr, int N)
{
// Stores bitwise AND
// of all possible pairs
int totalAND = (1 << 30) - 1;
// Iterate over the array arr[]
for(int i = 0; i < N; i++)
{
// Calculate bitwise AND
// of each array element
totalAND &= arr[i];
}
return totalAND;
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 4, 5, 12, 15 };
int N = arr.Length;
Console.Write(TotalAndPair(arr, N));
}
}
// This code is contributed by AnkThon
Java脚本
4
时间复杂度:O(N)
辅助空间: O(1)