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

📅  最后修改于: 2021-05-31 20:09:29             🧑  作者: 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如下所示:
  • 由于按位AND遵循Associative属性,因此上述表达式可以重新安排为:
  • 可以观察到,每个数组元素在所有可能的对中精确地(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)