Ctrl K

Magic 3×3 Blocks

Medium
You are given a rectangular board board of integers. Call a 3×3 block *magic* if it holds every digit from 1 to 9 exactly once and its three rows, three columns and both diagonals all have the same sum. Count how many magic 3×3 blocks (made of adjacent rows and columns) appear on the board. Blocks may overlap. The board may also contain zeros and numbers above 9; a block containing one of them is never magic.
Example 1
board=[[2, 7, 6, 2], [9, 5, 1, 9], [4, 3, 8, 4]] → 1

The left 3×3 block holds each digit 1–9 once, and every row, column and both diagonals add up to 15. The right block (columns 1–3) repeats the 9, so it does not count.

Example 2
board=[[5, 5, 5], [5, 5, 5], [5, 5, 5]] → 0

Every sum is 15, but the digits repeat, so the block is not magic.

Example 3
board=[[12, 3], [0, 7]] → 0

The board is smaller than 3×3, so there are no blocks at all.

Constraints
  • 1 ≤ number of rows, number of columns ≤ 10
  • All rows have the same length
  • 0 ≤ board[i][j] ≤ 15
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPrimes Along Grid Rays MediumMisprinted Bingo Card EasySudoku Board Without Clashes MediumTurn a Square Clockwise MediumZero Out the Crosses Medium
loading Python…
Input
board=[[2, 7, 6, 2], [9, 5, 1, 9], [4, 3, 8, 4]]
Expected
1

Run checks the first 3 tests in your browser. Submit checks all 76 tests on the server, hidden ones included.