📜  给定数组中所有无序对的按位与

📅  最后修改于: 2021-09-03 15:01:01             🧑  作者: Mango

给定一个大小为N的数组arr[] ,任务是找到给定数组中存在的所有可能的无序对的按位与。

例子:

天真的方法:这个想法是遍历数组并生成给定数组的所有可能对。最后,打印给定数组的这些对中存在的每个元素的按位与。请按照以下步骤解决问题:

  • 初始化一个变量,比如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 如下:
  • 由于 Bitwise AND 遵循Associative property ,上述表达式可以重新排列为:
  • 可以观察到,每个数组元素在所有可能的对中恰好出现(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

蟒蛇3

# 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)
辅助空间: O(1)

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