publicclassSolution {publicint[][] generateMatrix(int n) {int[][] matrix =newint[n][n];int x =0, y =0;int k = n;int val =1;while (k >0) {// left to rightfor (int i =0; i < k -1; i++) { matrix[x][y++] = val++; }// to bottomfor (int i =0; i < k -1; i++) { matrix[x++][y] = val++; }// to leftfor (int i =0; i < k -1; i++) { matrix[x][y--] = val++; }// to topfor (int i =0; i < k -1; i++) { matrix[x--][y] = val++; }// to next x++; y++; k -=2; }if (n %2!=0) { matrix[n /2][n /2] = val; } return matrix; }}