EVENTUAL - Editorial

from collections import Counter
for _ in range(int(input())):
N = int(input())
s = input()
dic = Counter(s)
flag = 0
for key in dic:
if dic[key]%2!=0:
flag = 1
if flag == 0:
print(“YES”)
else:
print(“NO”)

I am taking Xor of all elements and if all elements exists even no of times xor will be zero
otherwise it will be non zero.

Is my logic wrong?

NO, your logic is correct but implementation is wrong.
suppose you have [1,2,3] so if you are checking XOR ,XOR of (1,2) will be 3 ,then (3 XOR 3) will be zero.Here your answer will be “YES”.but it should be “NO”.So implement it in this way such that no two values XOR will be same to anyother value

For ‘a’ set fix the 0th bit,and for b fix the 1th bit,and for c fix the 2nd bit and so on.Doing this if any char comes 2nd times then simply set bit of that position for that element will become unset, and it will never coinside with any other because for every character you have been fixed a position.

There is a problem in your memset function.
This is the syntax of memset function.

void *memset(void *ptr, int x, size_t n);

The first argument is the array, the second argument is the value to be assigned to each element (which can only be 0 or -1) and the last argument is the sizeof array (in bytes).

Now for your code:
The array f contains 26 elements and the size of int is 4 bytes. So 26*4=104 bytes.
You can either use memset(f,0,104); or memset(f,0,sizeof(f)); .

It is always better to use this memset(arr,0,sizeof(arr));

1 Like

https://www.codechef.com/viewsolution/35847888
I tried to run this in VS. It seems everything correct, but I don’t know what is actually wrong here someone help me please.

Thanks brother. i was not aware of this.

The problem here is that you are assigning true to ans(bool) everytime you check a new element, which would just mask the information you will get about ans from the last example.
For eg, test case:
1
7
aaaaabb
Your code yields YES whereas, the answer should be NO.

Fix:

Assign true to ans only once , that is before the 20th line(for(i…))

#include
#include

using namespace std;

int main()
{
const int Index = 1e3;
int t, countnum;
cin >> t;
string s;
char temp;
bool ans; //should move this one
int position = 1;
while (t–)
{
int n;
cin >> n;
cin >> s;
ans = true;
for (int i = 0; i < n; i++)
{
temp = s[i];
countnum = 0;
for (int i = 0; i < n; i++)
{
if (temp == s[i])
{
countnum++;
}
}
//cout << countnum << endl;
if (countnum % 2 != 0)
{
ans = false;
}
}
if (ans)
{
cout << “YES\n”;
}
else
{
cout << “NO\n”;
}
}
}

okay thank you very much.

If i XOR all the elements of String then the final value of all the XOR should be 0 for even freq of each element and not equal to 0 if some element occurs odd no. of times. But this does not seem to work. Can you help me figure it out ?

can explain why am i getting wrong ans for this code for task eventual reduction.

void test_case(){
int n;
cin>>n;
string s;
cin>>s;
if(s.size()%2==1){
    cout<<"NO"<<endl;
    return;
}
map<int,int>mp;
for(auto x:s){
    mp[x]++;
}
for(auto x:mp){
    if(x.ss%2==1){
        cout<<"NO"<<endl;
        return;
    }
}
cout<<"YES"<<endl;

}

and this code:

int main() {
int t;
cin>>t;
while(t–)
{
int n,count=0;
string str;
cin>>n;
cin>>str;
unordered_map<char , int> freq;
for(const char &c : str){
freq[c]++ ;
}
for(const char &c : str){
if(freq[c]%2==0){
count++;
}
}
if(n%2==0&&count==n)
cout<<“YES”<<endl;
else
cout<<“NO”<<endl;
}
}

It should. Can you show me your code?

It won’t. Reduce the problem to this given an array of integers find if all integers occur even no. Of times and for the purpose of finding this I XOR all the elements and return XOR == 0 . However this approach fails for the given array
1, 2, 3

That is why we are associating the characters with powers of 2. {A:1, B:2, C:4 and so on}

Thank you so much!! @sumedhzope

from collections import Counter
try:
t=int(input())
if 1<=t<=200:
for T in range(t):
n=int(input())
if 1<n<=1000:
st=input()
if len(st)%2==1:
print(“NO”)
else:
d = Counter(st)
l = list(d.values())
temp = 0
for i in l:
if i % 2 == 0:
temp = 0
else:
temp = 1
break
if temp == 0:
print(“YES”)
else:
print(“NO”)

except:
pass

WHY IS THIS WRONG

// Can someone tell where my code fails, work perfectly with sample inputs.
#include <stdio.h>

int main(void) {
int t;
scanf("%d",&t);
while(t–)
{
int i,n,count=0;
char a[1000];
scanf("%d",&n);
for(i=1;i<=n;i++){
scanf(" %c",&a[i]);
}

 for(i=1;i<=n;i++){
     for(int j=1;j<=n;j++){
         if(a[i]==a[j] &&i!=j){
         count++;
         break;
         }
     }
}

if(count==n)
printf("YES\n");
else
printf("NO\n");
}
return 0;

}

hey folks , can somone tell whats wrong with this approach :sweat_smile:

#include <iostream>
using namespace std;

int main() {
	int t;
	cin>>t;

	while(t--){
	    int n ;
	    string str;
	    cin>>n;
	    cin.ignore();
	    getline(cin,str);
	    int *ar = new int[26];
	    
	    bool *visited = new bool[26];
	    for(int i =0;i<26;i++){
	        ar[i]=0;
	        visited[i]=false;
	    };
	    
	    for(int i =0;i<n;i++){
	        ar[str[i]-'a']++;
	    }
	   int c = 0;
	   for(int i =0;i<n;i++){
	       if(!visited[str[i]-'a'] && ar[str[i]-'a']%2==0){
	           visited[str[i]-'a']=true;
	           c+=ar[str[i]-'a'];
	       }
	   }
	   if(c==n){
	       cout<<"Yes\n";
	   }else{
	       cout<<"No\n";
	       
	   }
	}
	return 0;
}

it was mistake of outputs though :joy:

This is the best possible code for this Question, especially after reading the 4th input of sample Input
#include <bits/stdc++.h>
using namespace std;
void yourcrushlovesyouback(){
string MyCrush;
int HerCuteness;
cin>>HerCuteness;
cin>>MyCrush;
int DaddyIssues[26];

memset(DaddyIssues,0,sizeof(DaddyIssues));

for(int MyCharm=0;MyCharm<HerCuteness;MyCharm++)
    ++DaddyIssues[MyCrush[MyCharm]-'a'];

for(int SheSaidYes=0;SheSaidYes<26;SheSaidYes++){
    if(DaddyIssues[SheSaidYes]%2==1){
        cout<<"NO\n";
        return;
        cout<<"SHE SAID NO!"<<endl;
    }
}
cout<<"YES\n";
return;
cout<<"SHE SAID YES!"<<endl;

}
int main() {
// your code goes here
int MyConfessions=1;
cin>>MyConfessions;
while(MyConfessions–>0)
yourcrushlovesyouback();
return 0;
}

1 Like