You have given N numbers A_1 to A_N.
You have to find number of pairs for which XOR of pairs is greater than paired numbers.
Explanation:
This is a simple bruteforce problem.
Run a program for each pair and whenever you find XOR of pair greater than both pair
integer increase the count element by one.
Solution:
Setter's Solution
#include <bits/stdc++.h>
using namespace std;
int solve(vector<int>&v)
{
int count=0,n=v.size();
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if((v[i]^v[j])>v[i]&&(v[i]^v[j])>v[j])
count++;
}
}
return count;
}
int main() {
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
vector<int>v(n);
for(int i=0;i<n;i++)
cin>>v[i];
cout<<solve(v)<<"\n";
}
return 0;
}
Please give me suggestions if anything is unclear so that I can improve. Thanks
Excited with the name Brute Force, I have just coded the brute solution in one go. But, I’m not sure why, but the Python 3 (submitted under PYTH 3.6 instead of PYPY3 based on Python 3.5) code got Runtime Error! Then, I was looking for corner cases, tried running it to the limit constraints, but it didn’t work. I was unable to find the reason for a Runtime Error, here’s the brute force code in O(n^2): CodeChef: Practical coding for everyone
Then, I just tried to handle it with try and except, but then I have got a TLE (Time Limit Exceeded). Here’s the same code, with minor changes compared to my previous submission - CodeChef: Practical coding for everyone
I’m not sure why this implementation didn’t work for Python 3.6, while the same brute approach is given here in the editorial.
P.S.: Using sys.stdin.readline() instead of input() worked, and the O(n^2) code now gets AC. Not sure if input() is slower or not, but I guess I’ve found a new alternative to work with, in my future solutions with Python 3.
The only problem now left is that the same code which works for PYPY3 fails for PYTH 3.6 language selection from dropdown in CodeChef IDE. If anyone has a working Python 3 solution (submitted under PYTH 3.6 and not PYPY3), please let me know.
Any help and suggestions on the same are appreciated!
i have applied same logic but i got tle
your solution is also have o(n) time complexity as i have , can you explain why
here is my code link : CodeChef: Practical coding for everyone