Priya and AND Codechef Solution: Priya loves bitwise AND, but she hates programming. Help her solve this problem.
Given an array AA of size NN, let BijBij denote the bitwise AND of A[i]A[i] and A[j]A[j]. You have to find the number of pairs (i,j)(i,j), such that i<ji<j and Bij=A[i]Bij=A[i].
Input:
- The first line of the input contains a single integer TT denoting the number of test cases.
- The first line of each test case consists of a single integer NN, denoting the Size of Array AA.
- The second line of each test case contains NN space-separated integers A1,A2,A3…ANA1,A2,A3…AN.
Output:
For each test case, output a single line, count of such pairs.
Constraints
- 1≤T≤1001≤T≤100
- 1≤N≤1001≤N≤100
- 1≤A[i]≤1001≤A[i]≤100
Sample Input:
2 5 1 1 1 1 1 1 10
Sample Output:
10
Explanation
Example case 1: Number of valid pairs are -(1,2),(1,3),(1,4),(1,5),(2,3),(2,4),(2,5),(3,4),(3,5)(1,2),(1,3),(1,4),(1,5),(2,3),(2,4),(2,5),(3,4),(3,5) and (4,5)(4,5). Therefore, total valid pairs =10=10.
Example case 2: Since N=1N=1, therefore there are no valid pairs
Priya and AND CodeChef Solution in JAVA
import java.util.*; class Codechef { public static void main (String[] args) throws java.lang.Exception { Scanner s=new Scanner(System.in); int t=s.nextInt(); while(t-->0) { int n=s.nextInt(); int[] a=new int[n]; for(int i=0;i<n;i++) { a[i]=s.nextInt(); } if(n==1) { System.out.println(0); continue; } System.out.println(find(a)); } } public static int find(int[] a) { int count=0; for(int i=0;i<a.length;i++) { for(int j=i+1;j<a.length;j++) { if((a[i]&a[j])==a[i]) { count++; } } } return count; } }
Priya and AND CodeChef Solution in CPP
#include<iostream> #define ll long long using namespace std; int main() { ll t; cin>>t; while(t--) { ll n; cin>>n; ll arr[n]; for(int i=0;i<n;i++) { cin>>arr[i]; } ll count=0; for(int i=0;i<n;i++) { for(int j=0;j<i;j++) { if((arr[i]&arr[j])==arr[j]) { count++; } } } cout<<count<<endl; } return 0; }
Priya and AND CodeChef Solution in Python
for _ in range(int(input())): k=int(input()) p=list(map(int,input().split())) cnt=0 for i in range(k-1): for j in range(i+1,k): if((p[i] & p[j]) == p[i]): cnt+=1 print(cnt)
Disclaimer: The above Problem (Priya and AND) is generated by CodeChef but the solution is provided by Chase2learn.This tutorial is only for Educational and Learning purpose.