Matrix Similarity After Cyclic Shifts | Easy C++
Intuition\nThe problem requires us to cyclically shift the rows of a matrix and check if the matrix remains unchanged after these shifts. Specifically, odd-inde
# Intuition\nThe problem requires us to cyclically shift the rows of a matrix and check if the matrix remains unchanged after these shifts. Specifically, odd-indexed rows are right-shifted, and even-indexed rows are left-shifted. To determine if the matrix remains unchanged, we can simulate the shifts and compare the resulting matrix with the original.\n\n# Approach\n1. **Matrix Dimensions**: First, determine the dimensions of the matrix, `m` (number of rows) and `n` (number of columns).\n2. **Shift Calculation**:\n   - For even-indexed rows (0, 2, 4, ...), we perform a left cyclic shift by `k` positions.\n   - For odd-indexed rows (1, 3, 5, ...), we perform a right cyclic shift by `k` positions.\n3. **Comparison**:\n   - For even-indexed rows, compare each element in the row with the element that would be in that position after a left shift.\n   - For odd-indexed rows, compare each element in the row with the element that would be in that position after a right shift.\n4. **Final Check**: If all comparisons are true, return `true`; otherwise, return `false`.\n\n# Complexity\n\n- **Time complexity**: O(m.n)\n  - We traverse each element of the matrix once, resulting in a linear complexity with respect to the number of elements.\n- **Space complexity**: O(1)\n  - No additional space proportional to the input size is used, only a few extra variables.\n\n# Code\n```\nclass Solution {\npublic:\n    bool areSimilar(vector<vector<int>>& mat, int k) {\n        bool isSimilar = true;\n        \n        int m = mat.size(), n = mat[0].size();\n\n        for(int i = 0; i < m; i+=2) {\n            for(int j = 0; j < n; j++) {\n                isSimilar &= mat[i][j] == mat[i][(j+k)%n];\n            }\n        }\n        for(int i = 1; i < m; i+=2) {\n            for(int j = 0; j < n; j++) {\n                int y = j - (k%n);\n                y = y < 0 ? y + n : y;\n\n                isSimilar &= mat[i][j] == mat[i][y];\n            }\n        }\n        return isSimilar;\n    }\n};\n```
['C++']