Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
JavaScript Program to Check if all rows of a matrix are circular rotations of each other
A matrix in JavaScript is a 2-dimensional array where each row contains a fixed number of elements. In this tutorial, we'll learn how to check if all rows of a matrix are circular rotations of each other, meaning we can transform any row into another through left or right rotations.
Circular rotation means shifting elements: rotating [1, 2, 3] right gives [3, 1, 2], and rotating left gives [2, 3, 1].
Problem Understanding
Given a matrix, we need to determine if every row can be converted to the first row through circular rotations.
Example 1
Input: [[1, 2, 3], [2, 3, 1], [3, 1, 2]] Output: Yes
Explanation: Row 2 [2, 3, 1] is row 1 rotated left by 1. Row 3 [3, 1, 2] is row 1 rotated left by 2.
Example 2
Input: [[1, 2, 3], [2, 1, 3], [1, 2, 3]] Output: No
Explanation: Row 2 [2, 1, 3] cannot be converted to [1, 2, 3] through any rotation.
Approach
Our solution involves:
Create a rotation function to shift array elements
Compare each row with the first row
For non-matching rows, try all possible rotations
Return false if any row cannot match the first row after all rotations
Implementation
// Function to rotate array elements to the right
function rotate(arr) {
let l = 0;
let r = arr.length - 1;
while (l < r) {
// Swap elements using arithmetic operations
arr[l] += arr[r];
arr[r] = arr[l] - arr[r];
arr[l] = arr[l] - arr[r];
l++;
}
return arr;
}
// Function to check if all matrix rows are circular rotations
function checkCircularRotations(mat) {
let rows = mat.length;
let cols = mat[0].length;
// Check each row starting from row 1
for (let i = 1; i < rows; i++) {
let rotations = 0;
// Try up to 'cols' rotations
while (rotations < cols) {
let j = 0;
// Compare current row with first row
for (j = 0; j < cols; j++) {
if (mat[0][j] != mat[i][j]) {
break;
}
}
// If all elements match, move to next row
if (j == cols) {
break;
} else {
// Rotate current row and try again
mat[i] = rotate(mat[i]);
}
rotations++;
}
// If no rotation matched, return false
if (rotations == cols) {
return false;
}
}
return true;
}
// Test the function
let matrix = [
[1, 2, 3],
[2, 3, 1],
[3, 1, 2]
];
console.log("Matrix:", matrix);
console.log("Result:", checkCircularRotations(matrix) ? "Yes" : "No");
// Test with second example
let matrix2 = [
[1, 2, 3],
[2, 1, 3],
[1, 2, 3]
];
console.log("\nMatrix2:", matrix2);
console.log("Result:", checkCircularRotations(matrix2) ? "Yes" : "No");
Matrix: [ [ 1, 2, 3 ], [ 2, 3, 1 ], [ 3, 1, 2 ] ] Result: Yes Matrix2: [ [ 1, 2, 3 ], [ 2, 1, 3 ], [ 1, 2, 3 ] ] Result: No
How the Rotation Works
The rotate function shifts all elements one position to the right:
function demonstrateRotation() {
let arr = [1, 2, 3];
console.log("Original:", arr);
rotate(arr);
console.log("After 1 rotation:", arr);
rotate(arr);
console.log("After 2 rotations:", arr);
rotate(arr);
console.log("After 3 rotations:", arr);
}
demonstrateRotation();
Original: [ 1, 2, 3 ] After 1 rotation: [ 3, 1, 2 ] After 2 rotations: [ 2, 3, 1 ] After 3 rotations: [ 1, 2, 3 ]
Time and Space Complexity
| Complexity | Value | Explanation |
|---|---|---|
| Time | O(N × M²) | N rows, M columns, M rotations per row |
| Space | O(1) | No additional space used |
Conclusion
This solution efficiently checks if matrix rows are circular rotations by comparing each row with the first after trying all possible rotations. The algorithm uses in-place rotation with O(1) space complexity.
