Transform to Chessboard - Problem
Transform to Chessboard

You are given an n x n binary grid board filled with 0s and 1s. Your goal is to transform this board into a valid chessboard pattern using the minimum number of moves.

In each move, you can:
• Swap any two rows with each other
• Swap any two columns with each other

A chessboard pattern is a board where no two adjacent cells (horizontally or vertically) have the same value - just like a real chessboard where black and white squares alternate.

Return: The minimum number of moves needed to create a chessboard pattern, or -1 if it's impossible.

Example:
Input:  [[0,1,1,0],     Output: 2
         [0,1,1,0],
         [1,0,0,1],
         [1,0,0,1]]
This can be transformed into a chessboard by swapping 2 rows or 2 columns.

Input & Output

example_1.py — Basic Transform
$ Input: board = [[0,1,1,0],[0,1,1,0],[1,0,0,1],[1,0,0,1]]
Output: 2
💡 Note: The board has 2 unique row patterns: [0,1,1,0] and [1,0,0,1] which are complements. Same for columns. We can swap row 0 with row 2, and the result will be a valid chessboard pattern. Total moves needed: 2.
example_2.py — Already Valid
$ Input: board = [[0,1],[1,0]]
Output: 0
💡 Note: This 2x2 board is already a valid chessboard pattern where no adjacent cells have the same value. No moves needed.
example_3.py — Impossible Case
$ Input: board = [[1,1,0],[0,0,1],[0,0,1]]
Output: -1
💡 Note: This board cannot be transformed into a chessboard. There are more than 2 unique row patterns, and the patterns are not proper complements of each other.

Constraints

  • n == board.length
  • n == board[i].length
  • 2 ≤ n ≤ 30
  • board[i][j] is either 0 or 1
  • Only boards with exactly 2 complementary patterns can be transformed

Visualization

Tap to expand
Transform to Chessboard INPUT n x n Binary Grid (n=4) 0 1 1 0 0 1 1 0 1 0 0 1 1 0 0 1 R0 R1 R2 R3 [[0,1,1,0],[0,1,1,0], [1,0,0,1],[1,0,0,1]] Only 2 unique row patterns Goal: Make alternating pattern using row/col swaps Rows 0,1 identical Rows 2,3 identical ALGORITHM STEPS 1 Validate Board Check: only 2 row types, complementary patterns 2 Count Row Swaps Rows need: 0,1,0,1 or 1,0,1,0 Current: 0,0,1,1 needs 1 swap 3 Count Col Swaps Cols need: 0,1,0,1 or 1,0,1,0 Current: 0,1,1,0 needs 1 swap 4 Sum Minimum Swaps Total = row_swaps + col_swaps Total = 1 + 1 = 2 Swap Operations: Row Swap: R1 <--> R2 0110 swap 1001 Col Swap: C1 <--> C2 col 1 swap col 2 FINAL RESULT Valid Chessboard Pattern 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 Output: 2 minimum moves OK - Valid chessboard! No adjacent cells have same value 1 row + 1 col swap Key Insight: A valid chessboard has exactly 2 row patterns (complements of each other) and 2 column patterns. Count mismatches with target pattern. Swaps needed = mismatches/2. Each swap fixes 2 positions. If impossible (wrong counts or non-complementary patterns), return -1. TutorialsPoint - Transform to Chessboard | Optimal Solution
Asked in
Google 15 Facebook 12 Apple 8 Microsoft 6
31.0K Views
Medium Frequency
~25 min Avg. Time
780 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen