Problem · Matrix
Min Flips to Make Grid Palindrome
Learn this problemProblem statement
You're working on a data storage system that represents information in a binary grid. For error correction and symmetry, every row and column must be a palindrome.
Additionally, due to hardware constraints, the total number of 1s must be divisible by 4.
You may flip any bit, changing 0 to 1 or 1 to 0. Return the minimum number of flips needed to make the grid valid.
Function
minFlips(grid: int[][]) → intExamples
Example 1
grid = [[1, 0, 1], [0, 1, 0], [1, 0, 0]]return = 2Flip the bottom-right 0 to 1 and the center 1 to 0. The resulting grid is [[1, 0, 1], [0, 0, 0], [1, 0, 1]], whose rows and columns are palindromes and whose four 1s are divisible by 4. One flip cannot satisfy both requirements, so the minimum is 2.
Constraints
gridis a non-empty rectangular matrix.- Every value in
gridis either0or1.
More Google problems
- Deduplicate Logs: Keep FirstONSITE INTERVIEW · Seen Jul 2026
- Deduplicate Logs: Keep LatestONSITE INTERVIEW · Seen Jul 2026
- Find a Template Across Binary-Tree LeavesONSITE INTERVIEW · Seen Jul 2026
- Maximum Programmer-Problem MatchingONSITE INTERVIEW · Seen Jul 2026
- Minimum Direction ViolationsONSITE INTERVIEW · Seen Jul 2026
- Stream Latest Log VersionsONSITE INTERVIEW · Seen Jul 2026
- Stream Unique Logs in Timestamp OrderONSITE INTERVIEW · Seen Jul 2026
- Top-K IP Addresses from File RecordsONSITE INTERVIEW · Seen Jul 2026