Hello Programmers In this post, you will know how to solve the Priya and AND Codechef Solution.
One more thing to add, don’t directly look for the solutions, first try to solve the problems of Codechef by yourself. If you find any difficulty after trying several times, then you can look for solutions.
Problem
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 Solutions 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 Solutions 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 Solutions 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 BrokenProgrammers. This tutorial is only for Educational and Learning purpose.
Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.