Why it is wrong

can anyone tell me why my code is giving WA.
https://www.codechef.com/BITB2021/problems/UNIBIT

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define vi vector
#define mod 1e9+7
#define ff first
#define ss second
#define ins insert
#define endl “\n”
#define pie_op ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define tc int t;cin>>t;while(t–)
#define pb push_back
#define input(n) ll arr[n];for(int i1=0;i1<n;i1++)cin>>arr[i1]

int main()
{
pie_op;
tc
{
unordered_map<int,int>mp;
ll n;cin>>n;
input(n);
sort(arr,arr+n);
for(ll i=1;i<n;i++)
{
++mp[arr[i]];
}
for(int i=0;i<n;i++)
{
if(mp[arr[i]]==3)
{
cout<<arr[i]<<endl;
break;
}
}
}
return 0;
}

3 Likes

Bro i also got wrong answer for this.

Code

#include <bits/stdc++.h>
using namespace std;

int main(){
int t;
cin >> t;
while(t-- > 0){
map<long long, long long> mp;
long long n;
cin >> n;
for(long long i = 0; i<n; i++){
long long a;
cin >> a;
mp[a]++;
}
for(auto p : mp){
if(p.second == 3){
cout << p.first << “\n”;
}
}
}
return 0;
}

By seeing some accepted solutions
they used unordered_map instead of map
and ( == 3 ) condition was changed by ( % 5 != 0 )
even if u use (==3) with unordered_map it will give wrong answer .
solution got accepted when i changed map to unordered_map and (==3) to ( %5 != 0)
Why ? Mysterious , Isn’t it ?

Unordered map in c++ seems pretty messed up.
I tried a lot of things with your code, including suggestions given here(LINK) but to no success.
I would love to understand more about this issue.
is unordered map non deterministic?

i dont know whats happening but i am pretty much sure that this code should work.

@msiddhu bro can you help here what we are missing?

Input function index start from 0 and mp[arr[i]] start from 1. I tried fixing it but it still didn’t got fixed

try bit-manipulation.

PLEASE EXPLAIN WHAT ARE U SAYING?A SIMPLE MAP WITH CHECK FREQUENCY EQUALS 3 IS GIVING Wstrong textA WHY?emphasized text

i think it will not satisfy the time constraints.

try this:
#include <bits/stdc++.h>
using namespace std;
typedef long long L;

L func(L arr[], int n)
{
L result = 0;
L x, sum;
for (L i = 0; i < 63; i++) {
sum = 0;
x = (1 << i);
for (L j = 0; j < n; j++) {
if (arr[j] & x)
sum++;
}
if ((sum % 5) != 0)
result |= x;
}
return result;
}
int main()
{

int t;
cin>>t;
while(t–){
L n;
cin>>n;
L arr[n];
for(L i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<func(arr,n)<<endl;
}
}