title: java codility Max-Counters
body: <p>I have been trying to solve the below task:</p>
  
  <p>You are given N counters, initially set to 0, and you have two possible operations on them:</p>
  
  <pre><code>    increase(X) − counter X is increased by 1,
      max_counter − all counters are set to the maximum value of any counter.
  </code></pre>
  
  <p>A non-empty zero-indexed array A of M integers is given. This array represents consecutive operations:</p>
  
  <pre><code>    if A[K] = X, such that 1 ≤ X ≤ N, then operation K is increase(X),
      if A[K] = N + 1 then operation K is max_counter.
  </code></pre>
  
  <p>For example, given integer N = 5 and array A such that:</p>
  
  <pre><code>A[0] = 3
  A[1] = 4
  A[2] = 4
  A[3] = 6
  A[4] = 1
  A[5] = 4
  A[6] = 4
  </code></pre>
  
  <p>the values of the counters after each consecutive operation will be:</p>
  
  <pre><code>(0, 0, 1, 0, 0)
  (0, 0, 1, 1, 0)
  (0, 0, 1, 2, 0)
  (2, 2, 2, 2, 2)
  (3, 2, 2, 2, 2)
  (3, 2, 2, 3, 2)
  (3, 2, 2, 4, 2)
  </code></pre>
  
  <p>The goal is to calculate the value of every counter after all operations.</p>
  
  <pre><code>struct Results {
    int * C;
    int L;
  }; 
  </code></pre>
  
  <p>Write a function:</p>
  
  <pre><code>struct Results solution(int N, int A[], int M); 
  </code></pre>
  
  <p>that, given an integer N and a non-empty zero-indexed array A consisting of M integers, returns a sequence of integers representing the values of the counters.</p>
  
  <p>The sequence should be returned as:</p>
  
  <pre><code>    a structure Results (in C), or
      a vector of integers (in C++), or
      a record Results (in Pascal), or
      an array of integers (in any other programming language).
  </code></pre>
  
  <p>For example, given:</p>
  
  <pre><code>A[0] = 3
  A[1] = 4
  A[2] = 4
  A[3] = 6
  A[4] = 1
  A[5] = 4
  A[6] = 4
  </code></pre>
  
  <p>the function should return [3, 2, 2, 4, 2], as explained above.</p>
  
  <p>Assume that:</p>
  
  <pre><code>    N and M are integers within the range [1..100,000];
      each element of array A is an integer within the range [1..N + 1].
  </code></pre>
  
  <p>Complexity:</p>
  
  <pre><code>    expected worst-case time complexity is O(N+M);
      expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).
  </code></pre>
  
  <p>Elements of input arrays can be modified.</p>
  
  <p><strong>Here is my solution:</strong></p>
  
  <pre><code>import java.util.Arrays;
  
  class Solution {
      public int[] solution(int N, int[] A) {
  
          final int condition = N + 1;
          int currentMax = 0;
          int countersArray[] = new int[N];
  
          for (int iii = 0; iii &lt; A.length; iii++) {
              int currentValue = A[iii];
              if (currentValue == condition) {
                  Arrays.fill(countersArray, currentMax);
              } else {
                  int position = currentValue - 1;
                  int localValue = countersArray[position] + 1;
                  countersArray[position] = localValue;
  
                  if (localValue &gt; currentMax) {
                      currentMax = localValue;
                  }
              }
  
          }
  
          return countersArray;
      }
  }
  </code></pre>
  
  <p>Here is the code valuation:
  <a href="https://codility.com/demo/results/demo6AKE5C-EJQ/">https://codility.com/demo/results/demo6AKE5C-EJQ/</a></p>
  
  <p>Can you give me a hint what is wrong with this solution?</p>