Problem with "Priya and AND"

here is the link to the problem:

Approach- I have iterate for every element A[i] in the array A and for each element i am calculating its AND with each of the next elements…and if the AND gets equal to my A[i],then i am incrementing my count variable…but it gives me WA …please someone help me out

#include<bits/stdc++.h>
using namespace std;
#define lli long long int

int main()
{
int t;
cin>>t;
while(t- -)
{
int n;
cin>>n;
long long int count=0;
int a[n];
for (int i=0;i<n;i++)
{
cin>>a[i];
}
for (int i=0;i<n;i++)
{
for (int j=i+1;j<n;j++)
{
if (a[i]&a[j]==a[i])
count++;
}
}
cout<<count<<endl;
}
}

1 Like

(a[i]&a[j]) should be there instaed of a[i]&a[j] inside j loop

3 Likes

Hello @nalingoyal01,
Actually in your code you are doing (a[i] & a[j]
== a[i] ) which results in WA because & operator prefers reading == operator first.
This code (a[i] & a[j]) == a[i] will result in an AC :slight_smile:

4 Likes

Prefer using ( ) brackets while using Bitwise Operators. Operator Precedence can ruin your code even if your intended logic was correct.
A[i]&A[j] change it to (A[i]&A[j]).

1 Like

thanks man, i was not knowing about the superiority of == over & operator…learnt something new…thanks :smiley: :smiley:

1 Like

thanks man

1 Like