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.

Visualization

Tap to expand
Transform to Chessboard: Step-by-StepInput BoardScrambled PatternPattern AnalysisPattern A: 011Pattern B: 100āœ“ ComplementsValid for TransformCalculate MovesRow Swaps: 1Col Swaps: 1Total: 2 movesMinimum SwapsFinal ResultPerfect ChessboardAlgorithm Steps:1Identify unique patterns in rows and columns2Verify patterns are exact complements (0↔1)3Check pattern counts differ by ≤ 14Calculate minimum swaps for optimal arrangement5Return sum of row swaps + column swapsKey Mathematical InsightA board can become a chessboard if and only if:• Exactly 2 unique row patterns (complements)• Exactly 2 unique column patterns (complements)• Pattern counts balanced (differ by ≤ 1)Time: O(n²) | Space: O(n)
Understanding the Visualization
1
Pattern Discovery
Identify all unique row and column patterns in the board
2
Validity Check
Verify there are exactly 2 patterns for rows and columns, each being complements
3
Count Analysis
Ensure pattern counts are balanced (differ by at most 1)
4
Optimal Target
Determine which chessboard arrangement requires fewer swaps
5
Move Calculation
Count minimum row swaps and column swaps needed
Key Takeaway
šŸŽÆ Key Insight: Only boards with exactly 2 complementary patterns for both rows and columns can be transformed into chessboards. The mathematical approach provides O(n²) optimal solution.

Time & Space Complexity

Time Complexity
ā±ļø
O(n²)

Single pass through the nƗn board to analyze patterns

n
2n
⚠ Quadratic Growth
Space Complexity
O(n)

Space to store unique row and column patterns

n
2n
⚔ Linearithmic Space

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
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