Need help in Mixcolor

help
implementation
march18

#1

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*;
	    }
	    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* == a[i-1])
	        {
	            a* = (a* + max) % 1000000000;
	            c++;
	            max += a* % 1000000000;
	        }
	    }
	    cout<<c<<endl;
	}
	return 0;
}

#2

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


#3

https://www.codechef.com/viewsolution/17919387
i have made changes in ur solution u caan view and pls upvote


#4

And the reason is that a* 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.


#5

if we u still face problem ask freely


#6

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


#7

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* 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.


#8

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


#9

ur code in another way has also passed all the verdict


#10

wooah… thanks brother.