I bet you are clicking run , just click submit

solution

your code giving wrong answer for this TC:

1

4

zaxc

answer is “NO” but it is giving “YES”.

your code is getting accepted but mine is giving me wrong ans even after making the changes. can you tell me what’s wrong with my code?

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));**

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…))

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}

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;
```

}