title: update and print array element?
body: <p>You are given an array of N positive integers, A1, A2 ,…, AN. </p>
  
  <p>Also, given a Q updates of form:- i j: Update Ai = j. 1 ≤ i ≤ N.</p>
  
  <p>Perform all updates and after each such update report mode of the array. Therefore, return an array of Q elements, where ith element is mode of the array after ith update has been executed.</p>
  
  <p>Notes </p>
  
  <ul>
  <li>Mode is the most frequently occuring element on the array.</li>
  <li>If multiple modes are possible, return the smallest one.</li>
  <li>Update array input is via a Q*2 array, where each row represents a update.</li>
  </ul>
  
  <p>For example,</p>
  
  <pre><code>A=[2, 2, 2, 3, 3]
  
  Updates=    [ [1, 3] ]
              [ [5, 4] ]
              [ [2, 4] ]
  
  A = [3, 2, 2, 3, 3] after 1st update.
  3 is mode.
  
  A = [3, 2, 2, 3, 4] after 2nd update.
  2 and 3 both are mode. Return smaller i.e. 2.
  
  A = [3, 4, 2, 3, 4] after 3rd update.
  3 and 4 both are mode. Return smaller i.e. 3.
  
  Return array [3, 2, 3].
  Constraints 
  1 ≤ N, Q ≤ 105 
  1 ≤ j, Ai ≤ 109
  </code></pre>
  
  <p>What's wrong with my implementation using simple array?</p>
  
  <pre><code>  /**
       * @input A : Integer array
       * @input n1 : Integer array's ( A ) length
       * @input B : 2D integer array 
       * @input n21 : Integer array's ( B ) rows
       * @input n22 : Integer array's ( B ) columns
       * 
       * @Output Integer array. You need to malloc memory, and fill the length in len1
       */
      int* getMode(int* A, int n1, int** B, int n21, int n22, int *len1) {
          int *ans = (int *) malloc (sizeof(int)); // dynamic allocation of array 
          int x = 0,i,j;
          for(i = 0; i &lt; n1 ; i ++ )
              for(j = 0; j &lt; n21; j++)
                  A[B[j][0]] = B[j][1];
              // frequency calculate  
  
              int c[n1] ;
  
              for(i = 0; i &lt; n1; i++)
                  c[i] = 0;
  
              for(i = 0; i &lt; n1; i++)
                  c[A[i]]++;
  
              int mx = INT_MAX;
              int idx = -1, idx1 = -1;
  
              for(i = 0; i &lt; n1; i++){
                  if (mx  &lt; A[i]){
                      idx1 = idx;
                      mx = A[i];
                      idx = i;
                  }
  
              int p;
              if (A[idx]&gt; A[idx1])
                  p = A[idx];
              else
                  p = A[idx1];
  
              ans[x++] = p; }
  
          return ans; }
  </code></pre>
  
  <p>How can we solve it? Can anyone please suggest some answer?</p>