MaxCounters codility understanding
<p>I have wanted to try some challenges on Codility and started from beginning. All assignments were relatively easy up to the one called MaxCounters. I do not believe that this one is especially hard although it is the first one marked as not painless. </p>

<p>I have read the <a href="https://codility.com/programmers/task/max_counters/" rel="nofollow noreferrer">task</a> and started coding in C# language:</p>

<pre><code>public static int[] maxPart(int N, int[] A){
    int[] counters = new int[N];
    for(int i = 0; i &lt; A.Length; i++){
        for(int j = 0; j &lt; counters.Length; j++){
            if(A[i] == counters[j] &amp;&amp; (counters[j] &gt;= 1 &amp;&amp; counters[j] &lt;= N )){
                counters [j] = counters [j] + 1;
            }
            if(A[i] == N + 1 ){
                int tmpMax = counters.Max ();
                for(int h = 0; h &lt; counters.Length; h++){
                    counters [h] = tmpMax;
                }
            }
        }
    }
    return counters;
}
</code></pre>

<p>Having 3 loops of course makes it really slow, but lets leave it for later. My concern is how I understood this like this and all the other people see it like on this question <a href="https://stackoverflow.com/questions/18908056/algorithm-max-counters">here</a>. </p>

<p>From the assignment's description.</p>

<p>it has 2 actions:</p>

<ul>
<li>increase(X) − counter X is increased by 1,   </li>
<li>max counter − all counters are set to the maximum value of any
counter.</li>
</ul>

<p>which occur under conditions:</p>

<ul>
<li>if A[K] = X, such that 1 ≤ X ≤ N, then operation K is increase(X), </li>
<li>if A[K] = N + 1 then operation K is max counter.</li>
</ul>

<p>Both conditions are stated in the code above. Obviusly it is wrong but I am confused, and I do not know how could I understand it differently. </p>

<p>Why is this code wrong, what am I missing from task description?</p>

<p>One of the top rated answers looks like this:</p>

<pre><code>public int[] solution(int N, int[] A) {
    int[] result = new int[N];
    int maximum = 0;
    int resetLimit = 0;

    for (int K = 0; K &lt; A.Length; K++)
    {
        if (A[K] &lt; 1 || A[K] &gt; N + 1)
            throw new InvalidOperationException();

        if (A[K] &gt;= 1 &amp;&amp; A[K] &lt;= N)
        {
            if (result[A[K] - 1] &lt; resetLimit) {
                result[A[K] - 1] = resetLimit + 1;
            } else {
                result[A[K] - 1]++;
            }

            if (result[A[K] - 1] &gt; maximum)
            {
                maximum = result[A[K] - 1];
            }
        }
        else
        {
            // inefficiency here
            //for (int i = 0; i &lt; result.Length; i++)
            //    result[i] = maximum;
            resetLimit = maximum;
        }
    }

    for (int i = 0; i &lt; result.Length; i++)
        result[i] = Math.max(resetLimit, result[i]);

    return result;
}
</code></pre>

<p>This code results with 100% on Codility. </p>

<p>Question:</p>

<p>I would like to know how the author knew from the task to use <code>result[A[K] - 1]</code>? What would <code>resetLimit</code> represent?</p>

<p>Maybe I completely misunderstood the question due to my English I am not sure. I just can not go over it. </p>

<p>EDIT:</p>

<p>Based on my code provided, how did I misunderstood the assignment?  Generally I am asking for explanation of the problem. Whether to explain what needs to be done, or take the code as correct result and provide and explanation why is this done this way?</p>