Dsa
Matrix
Medium
Valid Matrix Color Pattern
Given a matrix representing colors where each color is represented by an integer, determine if the following rule holds: No two adjacent cells (horizontally or vertically) can have the same color.
Input format: The first line contains two integers, N (the number of rows) and M (the number of columns). The next N lines contain M integers each, indicating colors.
Output format: Print 'YES' if the matrix holds the rule, otherwise print 'NO'.
Example:
Input:
3 3
1 2 1
2 1 2
1 2 1
Output:
YES
Key concepts
matrixcoloringvalidity
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now