 # CHNGIT - Editorial

I’m having trouble finding error in this code: https://www.codechef.com/viewsolution/28469142

Custom input is giving correct answer, but getting WA on submission. Can someone please provide test case for which this code is not producing correct output.

How is the code different form this: https://www.codechef.com/viewsolution/28468971

Consider the testcase:

``````1
7
30 100 88 74 92 89 46
``````

Edit:

Since a_i can be 100, you have an out-of-bounds access at the line:

``````a[num]++;
``````

so the behaviour is Undefined.

Edit2:

``````1
10
100 100 100 100 100 100 100 100 100  2
``````

also gives the wrong answer, even with your AC submission - weak testcases, there @vijju123 @kmaaszraa - can we get this testcase added to the Practice version? 2 Likes

Actually the condition A_i \le N holds in all test cases. I didn’t want to fail any solutions because of this.

I don’t know if my logic is correct or not. Can anyone please help me out, my solution was accepted but after reading this editorial. I found my solution nowhere close to the discussion.

So this is what I did.
I found the frequency of element which has occured the most, say sequence is (1,4,6,1,3,6). So max frequency is 2 be it for 6 or 1 doesn’t matter.
Since we have to find the minimum number of moves so I substract the max frequency from size of sequnce. In above case the size is 6. so minimum number of swappings is 6-2=4

Let’s take One of the given testcase 9 8 1 8
so max frequency is 2 (we are not bothered to find which element has max frequemcy, we just need max frequency) so length is 4
Therefore 4-2=2

Can someone share there thoughts about this approach. It was accepted but still I’m confused if it’s right!

Some one help me with my solution. Sometime, it’s work, but it always runtime error
[https://www.codechef.com/viewsolution/28481127] is my solution(https://www.codechef.com/viewsolution/28481127)

I think so. But I have a problem, i don’t know how to fix that?

Here’s a random testcase your code fails on:

``````7
2
4 1
1
2
2
25 27
1
9
8
32 13 87 33 33 64 37 2
10
3 4 45 16 19 38 32 58 18 28
1
3
``````

thanks so much

1 Like

1
3
1
0
1
0
6
9
0
Is’s right?

On my machine, your solution gives:

``````1
0
1
0
6
9
-57
``````

so you’re obviously triggering some Undefined Behaviour somewhere.

Edit:

Ah - you’re not initialising any of the arrays:

`````` int mang[N],phantu[N],soluong[N];
``````

so their contents will be unpredictable.

1 Like

Oh, thank you for your suggestions.

1 Like

Could you please have a look on to this as well?

Looks like the same logic as used in the Editorial to me 1 Like

hello Can anyone tell me where I am getting the wrong answer in the following code :
#include <bits/stdc++.h>
using namespace std;

int main() {
int t,n;
cin>>t;
while(t–)
{
int input=0;int i,c=0,max,j;
cin>>n;
vectorv;
for(i=0;i<n;i++)
{
cin>>input;
v.push_back(input);
}
sort(v.begin(),v.end());
for(i=0;i<n;i++)
{
max=v[i];
for(j=i+1;j<n;j++)
{
if(max==v[j])
{
c++;

``````            }
}
}
if((c+1)==n)
cout<<0;
else if(c==0)
cout<<n-1;
else
cout<<n-(c+1);

}
return 0;
``````

}

Please either format your code or link to your submission - the forum software has mangled it and it won’t compile! I don’t understand whats wrong with this. Can someone please help me figure it out. It throws WA.

https://www.codechef.com/viewsolution/29014678

``````#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
ios::sync_with_stdio(0);
cin.tie(0);

int t;
cin >> t;
while(t--) {
int n;
cin >> n;
int x[n];
for(int i = 0; i < n; i++) {
cin >> x[i];
}
int ans = {0};
for(int i = 0; i < n; i++) {
ans[x[i]]++;
}
int largest = -1;
for(int i = 0; i < 100; i++) {
largest = max(ans[i], largest);
}
cout << n - largest << endl;
}
}
``````

Python Soln: (https://www.codechef.com/viewsolution/33146762)

could you explain what is the meaning of this line.

//can someone please tell me why is this code wrong? this is code link
https://www.codechef.com/viewsolution/36085318
#include < iostream>
using namespace std;

int main() {
int t;
cin >> t;
while (t–){
int n;
cin >> n;
char a[n];
for(int i=0; i<n; i++){
cin >> a[i];
}
int count=0, countmain=0,ele,step=0; // for finding max number of same same element.
for(int i=0; i<n; i++){ //count main is maxixmum no of same element
count=0;
for(int j=0; j<n; j++){
if(a[i]==a[j]) count++;
}
if (count > countmain){
countmain=count;
ele = a[i];
}
}
for(int j=0; j<=n; j++){
for(int i=0; i<n; i++){
if(a[i]==ele) continue;
else if (a[i-1]==ele){
a[i]=a[i-1];
step++;
}
else if (a[i+1]==ele) {
a[i]=a[i+1];
step++;
}
else continue;
}

``````    }
cout << step << endl;
}

return 0;
``````

}