Spin Rings Time : O(nm) Space : O(1) 12345678910111213141516171819202122void spinRings(vector<vector<int>> &array, int start = 0) { int n = array.size(), m = array[0].size(); if(min(n,m) / 2 == start) return; int sy = start, ey = n - start - 1, sx = start, ex = m - start - 1; int tmp = array[start][start]; for(int i = sy; i < ey; i++) { array[i][sx] = array[i+1][sx]; } for(int i = sx; i < ex; i++) { array[ey][i] = array[ey][i+1]; } for(int i = ey; i > sy; i--) { array[i][ex] = array[i-1][ex]; } for(int i = ex; i > sx; i--) { array[sy][i] = array[sy][i-1]; } array[sy][sx+1] = tmp; spinRings(array, start + 1);}