A textile designer represents a rectangular tapestry swatch as a grid with r rows and c columns, where each cell holds an integer thread-color code. The swatch is called a harmonious weave if, along every diagonal running from the upper-left toward the lower-right, all cells carry the same color code (a diagonal here means the set of cells (row, col) that share the same value of row - col). Given the swatch, decide whether it is a harmonious weave.
Line 1: two integers r and c.
Next r lines: each contains c integers, the color codes of that row, left to right.
Print true if the swatch is a harmonious weave, or false otherwise.
1 <= r, c <= 2000 <= color code <= 10^5Example 1
Input
3 4 1 2 3 4 5 1 2 3 9 5 1 2
Expected
true
Explanation
Every diagonal running top-left to bottom-right holds a single repeated value: the main diagonal is all 1s, the diagonal just above it is all 2s, the one above that is all 3, and so on for every diagonal in the swatch, so it is a harmonious weave.
Example 2
Input
2 2 1 2 2 2
Expected
false
Explanation
The diagonal through cell (0,0) also passes through cell (1,1); those two cells hold colors 1 and 2, which differ, so the swatch is not a harmonious weave.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →