📌  相关文章
📜  计算来自按位或大于按位AND的数组中的对

📅  最后修改于: 2021-05-18 00:01:25             🧑  作者: Mango

给定一个由N个整数组成的数组A [] ,任务是计算对(i,j)的对数,以使i ,并且A [i]A [j]的按位或大于A [i]A [j]

例子:

天真的方法:最简单的方法是从给定数组生成所有可能的对,并且对于每个对(i,j) ,如果满足给定条件,则将计数1 。检查所有对之后,将count的值作为结果打印。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count the number of
// pairs (i, j) their Bitwise OR is
// greater than Bitwise AND
void countPairs(int A[], int n)
{
    // Store the required answer
    int count = 0;
 
    // Check for all possible pairs
    for (int i = 0; i < n; i++) {
 
        for (int j = i + 1; j < n; j++)
 
            // If the condition satisfy
            // then increment count by 1
            if ((A[i] | A[j])
                > (A[i] & A[j])) {
                count++;
            }
    }
 
    // Print the answer
    cout << count;
}
 
// Driver Code
int main()
{
    int A[] = { 1, 4, 7 };
    int N = sizeof(A) / sizeof(A[0]);
 
    // Function Call
    countPairs(A, N);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
 
  // Function to count the number of
  // pairs (i, j) their Bitwise OR is
  // greater than Bitwise AND
  static void countPairs(int A[], int n)
  {
    // Store the required answer
    int count = 0;
 
    // Check for all possible pairs
    for (int i = 0; i < n; i++) {
 
      for (int j = i + 1; j < n; j++)
 
        // If the condition satisfy
        // then increment count by 1
        if ((A[i] | A[j])
            > (A[i] & A[j])) {
          count++;
        }
    }
 
    // Print the answer
    System.out.println(count);
  }
 
  // Driver Code
  public static void main(String args[])
  {
    int A[] = { 1, 4, 7 };
    int N = A.length;
 
    // Function Call
    countPairs(A, N);
  }
}
 
// This code is contributed by splevel62.


Python3
# Python program to implement
# the above approach
 
# Function to count the number of
# pairs (i, j) their Bitwise OR is
# greater than Bitwise AND
def countPairs(A, n):
   
    # Store the required answer
    count = 0;
 
    # Check for all possible pairs
    for i in range(n):
        for j in range(i + 1, n):
 
            # If the condition satisfy
            # then increment count by 1
            if ((A[i] | A[j]) > (A[i] & A[j])):
                count += 1;
 
    # Prthe answer
    print(count);
 
# Driver Code
if __name__ == '__main__':
    A = [1, 4, 7];
    N = len(A);
 
    # Function Call
    countPairs(A, N);
 
    # This code is contributed by 29AjayKumar


C#
// C# program to implement
// the above approach
using System;
class GFG
{
  // Function to count the number of
  // pairs (i, j) their Bitwise OR is
  // greater than Bitwise AND
  static void countPairs(int[] A, int n)
  {
    // Store the required answer
    int count = 0;
 
    // Check for all possible pairs
    for (int i = 0; i < n; i++) {
 
      for (int j = i + 1; j < n; j++)
 
        // If the condition satisfy
        // then increment count by 1
        if ((A[i] | A[j])
            > (A[i] & A[j])) {
          count++;
        }
    }
 
    // Print the answer
    Console.Write(count);
}
 
// Driver Code
public static void Main()
{
    int[] A = { 1, 4, 7 };
    int N = A.Length;
 
    // Function Call
    countPairs(A, N);
}
}
 
// This code is contributed by susmitakundugoaldanga.


C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count the number of
// pairs (i, j) their Bitwise OR is
// greater than Bitwise AND
void countPairs(int A[], int n)
{
    // Total number of pairs
    // possible from the array
    long long count = (n * (n - 1)) / 2;
 
    // Stores frequency of each array element
    unordered_map ump;
 
    // Traverse the array A[]
    for (int i = 0; i < n; i++) {
 
        // Increment ump[A[i]] by 1
        ump[A[i]]++;
    }
 
    // Traverse the Hashmap ump
    for (auto it = ump.begin();
         it != ump.end(); ++it) {
 
        // Subtract those pairs (i, j)
        // from count which has the
        // same element on index i
        // and j (i < j)
        long long c = it->second;
        count = count - (c * (c - 1)) / 2;
    }
 
    // Print the result
    cout << count;
}
 
// Driver Code
int main()
{
    int A[] = { 1, 4, 7 };
    int N = sizeof(A) / sizeof(A[0]);
 
    // Function Call
    countPairs(A, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
// Function to count the number of
// pairs (i, j) their Bitwise OR is
// greater than Bitwise AND
static void countPairs(int A[], int n)
{
    // Total number of pairs
    // possible from the array
    int count = (n * (n - 1)) / 2;
 
    // Stores frequency of each array element
    Map mp = new HashMap<>();
 
    // Traverse the array A[]
    for (int i = 0 ; i < n; i++){
        if(mp.containsKey(A[i])){
            mp.put(A[i], mp.get(A[i])+1);
        }
        else{
            mp.put(A[i], 1);
        }
    }
 
    // Traverse the Hashmap ump
    for (Map.Entry it : mp.entrySet()){
 
        // Subtract those pairs (i, j)
        // from count which has the
        // same element on index i
        // and j (i < j)
        int c = it.getValue();
        count = count - (c * (c - 1)) / 2;
    }
 
    // Print the result
    System.out.print(count);
}
 
// Driver Code
public static void main(String[] args)
{
    int A[] = { 1, 4, 7 };
    int N = A.length;
 
    // Function Call
    countPairs(A, N);
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python 3 program for the above approach
from collections import defaultdict
 
# Function to count the number of
# pairs (i, j) their Bitwise OR is
# greater than Bitwise AND
def countPairs(A, n):
 
    # Total number of pairs
    # possible from the array
    count = (n * (n - 1)) // 2
 
    # Stores frequency of each array element
    ump = defaultdict(int)
 
    # Traverse the array A[]
    for i in range(n):
 
        # Increment ump[A[i]] by 1
        ump[A[i]] += 1
 
    # Traverse the Hashmap ump
    for it in ump.keys():
 
        # Subtract those pairs (i, j)
        # from count which has the
        # same element on index i
        # and j (i < j)
        c = ump[it]
        count = count - (c * (c - 1)) // 2
 
    # Print the result
    print(count)
 
# Driver Code
if __name__ == "__main__":
 
    A = [1, 4, 7]
    N = len(A)
 
    # Function Call
    countPairs(A, N)
 
    # This code is contributed by chitranayal.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
 
  // Function to count the number of
  // pairs (i, j) their Bitwise OR is
  // greater than Bitwise AND
  static void countPairs(int[] A, int n)
  {
 
    // Total number of pairs
    // possible from the array
    long count = (n * (n - 1)) / 2;
 
    // Stores frequency of each array element
    Dictionary ump = new Dictionary();
 
    // Traverse the array A[]
    for (int i = 0; i < n; i++)
    {
 
      // Increment ump[A[i]] by 1
      if(ump.ContainsKey(A[i]))
      {
        ump[A[i]]++;
      }
      else{
        ump[A[i]] = 1;
      }
    }
 
    // Traverse the Hashmap ump
    foreach(KeyValuePair it in ump)
    {
 
      // Subtract those pairs (i, j)
      // from count which has the
      // same element on index i
      // and j (i < j)
      long c = it.Value;
      count = count - (c * (c - 1)) / 2;
    }
 
    // Print the result
    Console.WriteLine(count);
  }
 
  // Driver code
  static void Main() {
    int[] A = { 1, 4, 7 };
    int N = A.Length;
 
    // Function Call
    countPairs(A, N);
  }
}
 
// This code is contributed by divyeshrabadiya07.


输出:
3

时间复杂度: O(N 2 )
辅助空间: O(1)

有效的方法:为了优化上述方法,这个想法是基于观察的条件A |仅当AiAj不同时才满足Aj> Ai&Aj 。如果Ai等于Aj ,则Ai | |。 Aj = Ai&Aj 。条件Ai | Aj 从未相遇。因此,可以通过从可能的总对数中减去具有相等值的对数来解决该问题。请按照以下步骤解决问题:

  • 初始化一个变量,例如count ,用N *(N – 1)/ 2表示可能的对总数。
  • 初始化一个哈希图,比如说M,以存储每个数组元素的频率。
  • 遍历数组arr [] ,对于每个数组元素arr [i] ,将Marr [i]的频率增加1
  • 现在,遍历哈希图M,并针对每个键K及其对应的值X ,从计数中减去(X *(X – 1))/ 2的值。
  • 完成上述步骤后,打印count的值作为结果。

下面是上述方法的实现:

C++

// C++ program for the above approach
#include 
using namespace std;
 
// Function to count the number of
// pairs (i, j) their Bitwise OR is
// greater than Bitwise AND
void countPairs(int A[], int n)
{
    // Total number of pairs
    // possible from the array
    long long count = (n * (n - 1)) / 2;
 
    // Stores frequency of each array element
    unordered_map ump;
 
    // Traverse the array A[]
    for (int i = 0; i < n; i++) {
 
        // Increment ump[A[i]] by 1
        ump[A[i]]++;
    }
 
    // Traverse the Hashmap ump
    for (auto it = ump.begin();
         it != ump.end(); ++it) {
 
        // Subtract those pairs (i, j)
        // from count which has the
        // same element on index i
        // and j (i < j)
        long long c = it->second;
        count = count - (c * (c - 1)) / 2;
    }
 
    // Print the result
    cout << count;
}
 
// Driver Code
int main()
{
    int A[] = { 1, 4, 7 };
    int N = sizeof(A) / sizeof(A[0]);
 
    // Function Call
    countPairs(A, N);
 
    return 0;
}

Java

// Java program for the above approach
import java.util.*;
class GFG
{
 
// Function to count the number of
// pairs (i, j) their Bitwise OR is
// greater than Bitwise AND
static void countPairs(int A[], int n)
{
    // Total number of pairs
    // possible from the array
    int count = (n * (n - 1)) / 2;
 
    // Stores frequency of each array element
    Map mp = new HashMap<>();
 
    // Traverse the array A[]
    for (int i = 0 ; i < n; i++){
        if(mp.containsKey(A[i])){
            mp.put(A[i], mp.get(A[i])+1);
        }
        else{
            mp.put(A[i], 1);
        }
    }
 
    // Traverse the Hashmap ump
    for (Map.Entry it : mp.entrySet()){
 
        // Subtract those pairs (i, j)
        // from count which has the
        // same element on index i
        // and j (i < j)
        int c = it.getValue();
        count = count - (c * (c - 1)) / 2;
    }
 
    // Print the result
    System.out.print(count);
}
 
// Driver Code
public static void main(String[] args)
{
    int A[] = { 1, 4, 7 };
    int N = A.length;
 
    // Function Call
    countPairs(A, N);
}
}
 
// This code is contributed by shikhasingrajput

Python3

# Python 3 program for the above approach
from collections import defaultdict
 
# Function to count the number of
# pairs (i, j) their Bitwise OR is
# greater than Bitwise AND
def countPairs(A, n):
 
    # Total number of pairs
    # possible from the array
    count = (n * (n - 1)) // 2
 
    # Stores frequency of each array element
    ump = defaultdict(int)
 
    # Traverse the array A[]
    for i in range(n):
 
        # Increment ump[A[i]] by 1
        ump[A[i]] += 1
 
    # Traverse the Hashmap ump
    for it in ump.keys():
 
        # Subtract those pairs (i, j)
        # from count which has the
        # same element on index i
        # and j (i < j)
        c = ump[it]
        count = count - (c * (c - 1)) // 2
 
    # Print the result
    print(count)
 
# Driver Code
if __name__ == "__main__":
 
    A = [1, 4, 7]
    N = len(A)
 
    # Function Call
    countPairs(A, N)
 
    # This code is contributed by chitranayal.

C#

// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
 
  // Function to count the number of
  // pairs (i, j) their Bitwise OR is
  // greater than Bitwise AND
  static void countPairs(int[] A, int n)
  {
 
    // Total number of pairs
    // possible from the array
    long count = (n * (n - 1)) / 2;
 
    // Stores frequency of each array element
    Dictionary ump = new Dictionary();
 
    // Traverse the array A[]
    for (int i = 0; i < n; i++)
    {
 
      // Increment ump[A[i]] by 1
      if(ump.ContainsKey(A[i]))
      {
        ump[A[i]]++;
      }
      else{
        ump[A[i]] = 1;
      }
    }
 
    // Traverse the Hashmap ump
    foreach(KeyValuePair it in ump)
    {
 
      // Subtract those pairs (i, j)
      // from count which has the
      // same element on index i
      // and j (i < j)
      long c = it.Value;
      count = count - (c * (c - 1)) / 2;
    }
 
    // Print the result
    Console.WriteLine(count);
  }
 
  // Driver code
  static void Main() {
    int[] A = { 1, 4, 7 };
    int N = A.Length;
 
    // Function Call
    countPairs(A, N);
  }
}
 
// This code is contributed by divyeshrabadiya07.
输出:
3

时间复杂度: O(N)
辅助空间: O(N)