Created
December 30, 2025 10:59
-
-
Save SuryaPratapK/c566f1988a118370b71346a48a18504a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| int rowSum(vector<vector<int>>& grid,int& row,int& col){ | |
| int rowSum = grid[row][col] + grid[row][col+1] + grid[row][col+2]; | |
| if(rowSum != grid[row+1][col] + grid[row+1][col+1] + grid[row+1][col+2] or | |
| rowSum != grid[row+2][col] + grid[row+2][col+1] + grid[row+2][col+2]) | |
| return -1; | |
| return rowSum; | |
| } | |
| int colSum(vector<vector<int>>& grid,int& row,int& col){ | |
| int colSum = grid[row][col] + grid[row+1][col] + grid[row+2][col]; | |
| if(colSum != grid[row][col+1] + grid[row+1][col+1] + grid[row+2][col+1] or | |
| colSum != grid[row][col+2] + grid[row+1][col+2] + grid[row+2][col+2]) | |
| return -1; | |
| return colSum; | |
| } | |
| bool singleDigitUniqueElementsCheck(vector<vector<int>>& grid,int& row,int& col){ | |
| vector<bool> seen(10,false); | |
| for(int i=row;i<=row+2;++i){ | |
| for(int j=col;j<=col+2;++j){ | |
| if(grid[i][j]<1 or grid[i][j]>9 or seen[grid[i][j]]) | |
| return true; | |
| seen[grid[i][j]]=true; | |
| } | |
| } | |
| return false; | |
| } | |
| public: | |
| int numMagicSquaresInside(vector<vector<int>>& grid) { | |
| int r = grid.size(); | |
| int c = grid[0].size(); | |
| int count = 0; | |
| for(int i=0;i<r-2;++i){ | |
| for(int j=0;j<c-2;++j){ | |
| int sum = grid[i][j+2] + grid[i+1][j+1] + grid[i+2][j];//Right Diagonal | |
| if(sum != grid[i][j] + grid[i+1][j+1] + grid[i+2][j+2])//Left Diagonal check | |
| continue; | |
| if(rowSum(grid,i,j)!=sum or colSum(grid,i,j)!=sum or singleDigitUniqueElementsCheck(grid,i,j)) | |
| continue; | |
| count++; | |
| } | |
| } | |
| return count; | |
| } | |
| }; | |
| /* | |
| //JAVA | |
| class Solution { | |
| public int numMagicSquaresInside(int[][] grid) { | |
| int r = grid.length, c = grid[0].length; | |
| int count = 0; | |
| for (int i = 0; i <= r - 3; i++) { | |
| for (int j = 0; j <= c - 3; j++) { | |
| if (isMagic(grid, i, j)) { | |
| count++; | |
| } | |
| } | |
| } | |
| return count; | |
| } | |
| private boolean isMagic(int[][] g, int r, int c) { | |
| // Center must be 5 | |
| if (g[r + 1][c + 1] != 5) return false; | |
| boolean[] seen = new boolean[10]; | |
| for (int i = r; i < r + 3; i++) { | |
| for (int j = c; j < c + 3; j++) { | |
| int v = g[i][j]; | |
| if (v < 1 || v > 9 || seen[v]) return false; | |
| seen[v] = true; | |
| } | |
| } | |
| int sum = g[r][c] + g[r][c + 1] + g[r][c + 2]; | |
| // Rows | |
| for (int i = 0; i < 3; i++) { | |
| if (g[r + i][c] + g[r + i][c + 1] + g[r + i][c + 2] != sum) | |
| return false; | |
| } | |
| // Columns | |
| for (int j = 0; j < 3; j++) { | |
| if (g[r][c + j] + g[r + 1][c + j] + g[r + 2][c + j] != sum) | |
| return false; | |
| } | |
| // Diagonals | |
| if (g[r][c] + g[r + 1][c + 1] + g[r + 2][c + 2] != sum) | |
| return false; | |
| if (g[r][c + 2] + g[r + 1][c + 1] + g[r + 2][c] != sum) | |
| return false; | |
| return true; | |
| } | |
| } | |
| #Python | |
| class Solution: | |
| def numMagicSquaresInside(self, grid): | |
| r, c = len(grid), len(grid[0]) | |
| count = 0 | |
| for i in range(r - 2): | |
| for j in range(c - 2): | |
| if self.isMagic(grid, i, j): | |
| count += 1 | |
| return count | |
| def isMagic(self, g, r, c): | |
| # Center must be 5 | |
| if g[r+1][c+1] != 5: | |
| return False | |
| seen = set() | |
| for i in range(r, r+3): | |
| for j in range(c, c+3): | |
| v = g[i][j] | |
| if v < 1 or v > 9 or v in seen: | |
| return False | |
| seen.add(v) | |
| s = g[r][c] + g[r][c+1] + g[r][c+2] | |
| # Rows & Columns | |
| for k in range(3): | |
| if sum(g[r+k][c:c+3]) != s: | |
| return False | |
| if g[r][c+k] + g[r+1][c+k] + g[r+2][c+k] != s: | |
| return False | |
| # Diagonals | |
| if g[r][c] + g[r+1][c+1] + g[r+2][c+2] != s: | |
| return False | |
| if g[r][c+2] + g[r+1][c+1] + g[r+2][c] != s: | |
| return False | |
| return True | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment