Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
You are given N integers: A1, A2, …, AN. You need to count the number of pairs of indices (i, j) such that 1 ≤ i < j ≤ N and Ai | Aj ≤ max(Ai, Aj).
Note: Ai | Aj refers to bitwise OR.
For each test case, output a single line containing the answer for that test case.
Subtask #1 (20 points):
Subtask #2 (80 points):
Input:
1
3
1 2 3
Output:
2
There are three possible pairs of indices which satisfy 1 ? i < j ? N: (1, 2), (1, 3) and (2, 3). Let us see which of those satisfy Ai | Aj ? max(Ai, Aj):
So there are a total of 2 valid pairs, and hence the answer is 2.
for _ in range(int(input())):
n = int(input())
arr = list(map(int, input().strip().split()))
d = {}
for i in range(n):
if arr[i] not in d:
d[arr[i]] = 1
else:
d[arr[i]] += 1
ans = 0
for A1 in d.keys():
ans += d[A1] * (d[A1] - 1)
for A2 in d.keys():
if A1 != A2 and (A1 | A2) <= max(A1, A2):
ans += d[A1] * d[A2]
print(ans // 2)
#include<bits/stdc++.h>
#include<ostream>
using namespace std;
#define int long long
#define sz(x) ((int)x.size())
#define all(x) (x).begin(), (x).end()
const int INF = numeric_limits<int>::max();
const int nax = (int)(3000901);
const int mod = 1e9 + 7;
template<class X, class Y>
bool maximize(X& x, const Y y) {
if (y > x) {x = y; return true;}
return false;
}
template<class X, class Y>
bool minimize(X& x, const Y y) {
if (y < x) {x = y; return true;}
return false;
}
int arr[nax], dulpet[nax];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int tt;
cin >> tt;
while (tt--) {
int n;
cin >> n;
memset(dulpet, 0, sizeof dulpet);
for (int i = 1; i <= n; ++i) {
cin >> arr[i];
dulpet[arr[i]]++;
}
int ans = 0;
for (int mask = 0; mask < (1 << 20); ++mask) {
ans -= (dulpet[mask] - 1) * dulpet[mask] / 2;
}
for (int i = 0; i < 20; ++i) {
for (int mask = 0; mask < (1 << 20); ++mask) {
if (mask & (1 << i)) {
dulpet[mask] += dulpet[mask ^ (1 << i)];
}
}
}
for (int i = 1; i <= n; ++i) {
ans += dulpet[arr[i]] - 1;
}
cout << ans << '\n';
}
return 0;
}
In our experience, we suggest you solve this Good Pairs CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.
If you are stuck anywhere between any coding problem, just visit Queslers to get the Good Pairs CodeChef Solution
I hope this Good Pairs CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.
This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.
Keep Learning!
More Coding Solutions >>