You are not logged in. Please login at to post your questions!


Need help in Mixcolor

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;
    while(t --)
        long long int n;
        long long int a[n];
        for(long long int i=0;i<n;i++)
        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;
                max += a[i] % 1000000000;
    return 0;

asked 20 Mar '18, 16:20

montycs's gravatar image

accept rate: 0%

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


answered 20 Mar '18, 16:31

gyanendra371's gravatar image

accept rate: 33%

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

(20 Mar '18, 16:34) gyanendra3713★

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.

(20 Mar '18, 16:40) gyanendra3713★

if we u still face problem ask freely

(20 Mar '18, 16:57) gyanendra3713★

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

(20 Mar '18, 17:21) montycs0★

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.

(20 Mar '18, 17:45) gyanendra3713★

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

(20 Mar '18, 17:54) gyanendra3713★

ur code in another way has also passed all the verdict

(20 Mar '18, 17:56) gyanendra3713★

wooah.. thanks brother.

(20 Mar '18, 18:01) montycs0★
showing 5 of 8 show all
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 20 Mar '18, 16:20

question was seen: 129 times

last updated: 20 Mar '18, 18:01