给定一个由N 个整数组成的数组A[] ,任务是计算对(i, j)的数量,使得i < j ,并且A[i]和A[j] 的按位或大于A[i]和A[j] 。
例子:
Input: A[] = {1, 4, 7}
Output: 3
Explanation:
There are 3 such pairs: (1, 4), (4, 7), (1, 7).
1) 1 | 4 (= 5) > 1 & 4 (= 0)
2) 4 | 7 (= 7) > 4 & 7 (= 4)
3) 1 | 7 (= 7) > 1 & 7 (= 1)
Input: A[] = {3, 3, 3}
Output: 0
Explanation: There exist no such pair.
朴素的方法:最简单的方法是从给定的数组中生成所有可能的对,对于每一对(i, j) ,如果它满足给定的条件,则将计数增加1 。检查所有对后,打印计数值作为结果。
下面是上述方法的实现:
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.
Javascript
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)
高效方法:为了优化上述方法,其思想是基于观察条件Ai | Aj > Ai & Aj只有当Ai和Aj不同时才满足。如果Ai等于Aj ,则Ai | Aj = Ai & Aj 。条件Ai | Aj < Ai & Aj从未相遇。因此,可以通过从可能的总对数中减去具有相等值的对数来解决该问题。请按照以下步骤解决问题:
- 初始化一个变量,比如count ,用N * (N – 1) / 2表示可能对的总数。
- 初始化一个 hashmap,比如M来存储每个数组元素的频率。
- 遍历数组arr[] ,对于每个数组元素arr[i] ,将arr[i]在M 中的频率增加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
蟒蛇3
# 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)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。