Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given an m x n integer matrix mat and an integer k. The matrix rows are 0-indexed.
The following proccess happens k times:
Return true if the final modified matrix after k steps is identical to the original matrix, and false otherwise.
Example 1:
Input: mat = [[1,2,3],[4,5,6],[7,8,9]], k = 4
Output: false
Explanation:
In each step left shift is applied to rows 0 and 2 (even indices), and right shift to row 1 (odd index).
Example 2:
Input: mat = [[1,2,1,2],[5,5,5,5],[6,3,6,3]], k = 2
Output: true
Explanation:
Example 3:
Input: mat = [[2,2],[2,2]], k = 3
Output: true
Explanation:
As all the values are equal in the matrix, even after performing cyclic shifts the matrix will remain the same.
Constraints:
1 <= mat.length <= 251 <= mat[i].length <= 251 <= mat[i][j] <= 251 <= k <= 50Problem summary: You are given an m x n integer matrix mat and an integer k. The matrix rows are 0-indexed. The following proccess happens k times: Even-indexed rows (0, 2, 4, ...) are cyclically shifted to the left. Odd-indexed rows (1, 3, 5, ...) are cyclically shifted to the right. Return true if the final modified matrix after k steps is identical to the original matrix, and false otherwise.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
[[1,2,3],[4,5,6],[7,8,9]] 4
[[1,2,1,2],[5,5,5,5],[6,3,6,3]] 2
[[2,2],[2,2]] 3
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2946: Matrix Similarity After Cyclic Shifts
class Solution {
public boolean areSimilar(int[][] mat, int k) {
int m = mat.length, n = mat[0].length;
k %= n;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (i % 2 == 1 && mat[i][j] != mat[i][(j + k) % n]) {
return false;
}
if (i % 2 == 0 && mat[i][j] != mat[i][(j - k + n) % n]) {
return false;
}
}
}
return true;
}
}
// Accepted solution for LeetCode #2946: Matrix Similarity After Cyclic Shifts
func areSimilar(mat [][]int, k int) bool {
n := len(mat[0])
k %= n
for i, row := range mat {
for j, x := range row {
if i%2 == 1 && x != mat[i][(j+k)%n] {
return false
}
if i%2 == 0 && x != mat[i][(j-k+n)%n] {
return false
}
}
}
return true
}
# Accepted solution for LeetCode #2946: Matrix Similarity After Cyclic Shifts
class Solution:
def areSimilar(self, mat: List[List[int]], k: int) -> bool:
n = len(mat[0])
for i, row in enumerate(mat):
for j, x in enumerate(row):
if i % 2 == 1 and x != mat[i][(j + k) % n]:
return False
if i % 2 == 0 and x != mat[i][(j - k + n) % n]:
return False
return True
// Accepted solution for LeetCode #2946: Matrix Similarity After Cyclic Shifts
fn are_similar(mat: Vec<Vec<i32>>, k: i32) -> bool {
let rows = mat.len();
let cols = mat[0].len();
let mut m = vec![vec![0; cols]; rows];
for i in 0..rows {
for j in 0..cols {
let col = (j + k as usize) % cols;
m[i][col] = mat[i][j];
}
}
mat == m
}
fn main() {
let mat = vec![vec![1, 2, 1, 2], vec![5, 5, 5, 5], vec![6, 3, 6, 3]];
let ret = are_similar(mat, 2);
println!("ret={ret}");
}
#[test]
fn test() {
{
let mat = vec![vec![1, 2, 1, 2], vec![5, 5, 5, 5], vec![6, 3, 6, 3]];
let ret = are_similar(mat, 2);
assert!(ret);
}
{
let mat = vec![vec![2, 2], vec![2, 2]];
let ret = are_similar(mat, 3);
assert!(ret);
}
{
let mat = vec![vec![1, 2]];
let ret = are_similar(mat, 1);
assert!(!ret);
}
}
// Accepted solution for LeetCode #2946: Matrix Similarity After Cyclic Shifts
function areSimilar(mat: number[][], k: number): boolean {
const m = mat.length;
const n = mat[0].length;
k %= n;
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (i % 2 === 1 && mat[i][j] !== mat[i][(j + k) % n]) {
return false;
}
if (i % 2 === 0 && mat[i][j] !== mat[i][(j - k + n) % n]) {
return false;
}
}
}
return true;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.