I am trying to solve MIXCOLOR and my code passes all the given test cases also the one’s that I am giving it but it is still giving me WA on codechef judge. Please help me in finding the error in my code or my understanding.

My code:

```
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int t;
cin>>t;
while(t --)
{
long long int n;
cin>>n;
long long int a[n];
for(long long int i=0;i<n;i++)
{
cin>>a[i];
}
sort(a,a+n);
long long int max = a[n-1];
long long int c = 0;
for(long long int i=1;i<n;i++)
{
if(a[i] == a[i-1])
{
a[i] = (a[i] + max) % 1000000000;
c++;
max += a[i] % 1000000000;
}
}
cout<<c<<endl;
}
return 0;
}
```

U need to make changes in a[i-1] not in a[i] and after that u will get write verdict.

2 Likes

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

i have made changes in ur solution u caan view and pls upvote

1 Like

And the reason is that a[i] need to be compared to a[i+1] as we need to change all the repeating element;

ex 2 2 2 3 by ur code it will change a[1] and it won’t be then equal to a[2] which is also 2.

1 Like

if we u still face problem ask freely

1 Like

Thanks brother… I just don’t get how is that making a big difference… I was updating a[i] since I thought a[i-1] was already a unique color… Please correct me if I am wrong…

when u sort array u get all those no together which are not unique like 1 2 2 2 3 3 7 7 so if u update a[i] in place of a[i-1] then u r gonna miss which are a[i+1].

we can easily understand it by example as here a[1]==a[2] so if u update a[2] then u will miss a[3] which is also 2(in this case);

a[3] and a[1] will remain same.

1 Like

if u r still confused then let me tell u another approach to his question this question was never meant to update value it just need us to count the no of repeating element after sorting (like 2 2 2 2 so answer would be 3).and ur code in another way https://www.codechef.com/viewsolution/17919825

1 Like

ur code in another way has also passed all the verdict

1 Like