title: Rotate each ring of the matrix M*N anticlockwise
body: <p>I am not able to rotate a <code>M*N</code> matrix in anticlockwise direction. My code is working properly for <code>3*3</code> matrix, but when I try for any other case it is not working assume I am doing it for <code>4*4</code> matrix then only outer elements are rotating and inner 4 elements (i.e. 6,7,10,11) are not rotating. My input is 1-16 numbers as <code>4*4</code> matrix:</p>
  <pre><code>{ {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16} }
  </code></pre>
  <pre class="lang-java prettyprint-override"><code>static void antiRotateMatrix(int m, int n, int mat[][]) {
      int row = 0, col = n - 1;
      int prev, curr;
      while (row &lt; m &amp;&amp; col &lt; n) {
          if (row + 1 == m || col - 1 == 0) {
              break;
          }
          prev = mat[row + 1][col];
          for (int i = col; i &gt;= 0; i--) {
              curr = mat[row][i];
              mat[row][i] = prev;
              prev = curr;
          }
          row++;
  
          for (int i = row; i &lt; m; i++) {
              curr = mat[i][0];
              mat[i][0] = prev;
              prev = curr;
          }
          n--;
  
          if (row &lt; m) {
              for (int i = n - 2; i &lt;= col; i++) {
                  curr = mat[m - 1][i];
                  mat[m - 1][i] = prev;
                  prev = curr;
              }
          }
          m--;
          if (col &lt;= n) {
              for (int i = m - 1; i &gt;= row; i--) {
                  curr = mat[i][col];
                  mat[i][col] = prev;
                  prev = curr;
              }
          }
          col++;
      }
      for (int i = 0; i &lt;= 3; i++) {
          for (int j = 0; j &lt;= 3; j++)
              System.out.print(mat[i][j] + &quot; &quot;);
          System.out.print(&quot;\n&quot;);
      }
  }
  </code></pre>
  <p>For <code>4*4</code> I should get</p>
  <pre><code>{{2,3,4,8},{1,7,11,12},{5,6,10,16},{9,13,14,15}}
  </code></pre>
  <p>but I am getting</p>
  <pre><code>{{2,3,4,8},{1,6,7,12},{5,10,11,16},{9,13,14,15}}
  </code></pre>