A bitwise pair

1.9

15 votes
Algorithms, Basics of Greedy Algorithms, C++, Greedy Algorithms
Problem

You are given an array A of N positive integers. Find the number of pairs (i,j) such that:

  • (A[i] | A[j])(A[i] & A[j]) = (A[i]A[j]), where | is a bitwise OR operator and & is a bitwise AND operator.

Input format

  • The first line contains an integer T denoting the number of test cases.
  • The first line of each test case contains an integer N.
  • The second line of each test case contains N space-separated integer denoting array A .

Output format

For each test case, print the number of pairs, in a new line, satisfying the equation.

Constraints

1T101N1051A[i]103

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Valid pairs (i,j) are :-

  • (1,1)
  • (1,2)
  • (1,3)
  • (2,1)
  • (2,2)
  • (2,3)
Editor Image

?