JavaScript Program to Check if all rows of a matrix are circular rotations of each other


Matrix is a kind of 2-D array in which there is an array of fixed arrays that defines the rows and for each index of this array there are fixed length arrays present and the length of these arrays defines the number of columns present in the matrix. We can store any kind of data type in these cells provided by the matrix.

We will be provided with a matrix and each row contains some integers and we have to check if each row is the rotation of one another or not. Rotation of each other means by some number or left or right rotations we can produce the same combination of each row.

Example 1

Let us assume the given matrix is:

mat = [ [1, 2, 3],
   [2, 3, 1],
   [3, 1, 2]]
Output: Yes

Explanation: Assuming the first row is constant and rotating the remaining one we can get the result as:

By rotating the second row one time to the right and rotating the second row two times to the right we can make both same as the first row.

Example 2

mat = [ [1, 2, 3],
   [ 2, 1, 3],
   [ 1, 2, 3]]
Output: No

Explanation: In the above matrix, the first and third rows are the same but we cannot convert the second row equals to the first by any number of rotations.

Approach

We have seen a proper example for understanding of the problem now let us see the steps to implement the code −

  • First, we will define a function rotate, to rotate the elements of the array passed as parameter to it using the two pointers and swapping technique.

  • After that we will define the check function and will pass the given matrix to check.

  • In the function, first we will get the length of the matrix by getting rows and columns and using the for loop we will check for each row from 1 to last with the 0th row.

  • If the current row is same as the first row then we will pass to the next row.

  • Otherwise, we will call the rotate function and rotate the given row to its next rotation.

  • We will do this process until we find the same array as the zeroth row or length of columns numbers of times.

  • If the current row is not equal to the zeroth even after the maximum rotations then we will return false.

  • If all the rows eventually become equal then we will return true.

Example

In the below example, we check if all rows of a matrix are circular rotations of each other. The input and expected output is given below.

Input: matrix = [ [ 1, 2, 3 ], [ 2, 3, 1 ], [ 3, 1, 2 ] ]

Output: Yes

// function to rotate the given array
function rotate(arr){
   var l = 0;
   var r = arr.length-1;
   while(l < r){
      arr[l] += arr[r];
      arr[r] = arr[l]-arr[r];
      arr[l] = arr[l]-arr[r];
      l++;
   }
   return arr;
}

// function to check if the given matrix can have the same rows
// after the certain number of rotations
function check(mat){

   // getting number of rows
   var rows = mat.length
   
   // getting number of columns
   var cols = mat[0].length
   
   // traversing over the each row of given matrix
   for(var i = 1; i < rows; i++){
      var k = 0;
      while(k < cols) {
         var j = 0;
         for(j = 0; j<cols; j++){
            if(mat[0][j] != mat[i][j]){
               break;
            }
         }
         if(j == cols){
            break;
         }
         else{
            mat[i] = rotate(mat[i]);
         }
         k++;
      }
      if(k == cols){
         return false;
      }
   }
   return true;
}

// defining the matrix
var mat = [ [1, 2, 3],
   [2, 3, 1],
   [3, 1, 2]];
console.log("The given matrix is: ");
console.log(mat);
if(check(mat) == true){
   console.log("Yes, all the rows of the matrix are circular rotation of each other");
}
else{
   console.log("NO, all the rows of the matrix are not in the circular rotation of each other");
}

Output

The given matrix is: 
[ [ 1, 2, 3 ], [ 2, 3, 1 ], [ 3, 1, 2 ] ]
Yes, all the rows of the matrix are circular rotation of each other

Time and Space Complexity

The time complexity of the above code is O(N*M*M) where N is the number or rows and M is the number of columns present in the given matrix. We are traversing over the matrix row wise gives the factor or N and for comparison and rotations of the row’s givens the factor or M*M.

The space complexity of the above code is O(1), as we are not using any extra space.

Conclusion

In this tutorial, we have implemented the JavaScript program to check if all the rows of the given matrix are circular rotations of each other or not by rotating every row and comparing with the zeroth row. We have use the two pointers and swap method to rotate the rows of the given matrix. The time complexity of the above code is O(N*M*M) and the space complexity is O(1).

Updated on: 13-Apr-2023

106 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements