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


The aim of this article is to implement a program C++ program to check if all rows of a matrix are circular rotations of each other. Here is a small glimpse on what a matrix exactly is.

The rectangular array of symbols or numbers organized in rows and columns is known as a matrix. A matrix can be of many distinct types, including row, column, horizontal, vertical, square, diagonal, identity, equal, and singular. Addition, subtraction, as well as multiplication are the three fundamental matrix operations.

The goal is to determine whether all rows of a matrix of size n*n are circular spins or rotations of one another or not.

Example 1

Let us take the Input matrix :
m[][] = 9, 8, 7
        7, 9, 8
        7, 8, 9
The Output obtained is : Yes

Explanation − Here Every row has been rotated by permutation of each other.

Example 2

Let us take the Input matrix : 
m[][] = 9, 8, 7
        7, 9, 8
        7, 8, 9
The Output obtained is : No

Explanation − Here you can observe that 7,8,9 is not the rotated permutation of 7,9,8. Hence the result is "No"

Example 3

Let us take the Input matrix:
m[][] =  3, 4, 5
         5, 3, 4
         4, 5, 3
The Output obtained is : Yes

Explanation − Here Every row has been rotated by permutation of each other.

Example 4

Let us take the Input matrix:
m[][] =  3, 4, 5
         4, 3, 5
         5, 4, 3
The Output obtained is : No

Explanation − Here you can observe that 4,3,5 is not the rotated permutation of 3,4,5. Hence the result is "No"

Problem Statement

Implement a C++ Program to Check if all rows of a matrix are circular rotations of each other

Approach

The approach to solve this problem and obtain a C++ program to check if all rows of a matrix are circular rotations of each other. We do the following steps.

In order to ensure that string searching operatioms are able to be carried out effectively, create a string made up of the first row's items then concatenate the string by itself. Please call this concatenated string as concatenatedStr.

Now go through the remaining rows. Make a string called currStr that contains the current row elements for each row being traversed. Return false if the current string, that is currStr, is not a substring of the concatenated string, that is the concatenatedStr.

Algorithm

The algorithm to implement a C++ Program to Check if all rows of a matrix are circular rotations of each other is given below −

  • Step 1 − Define a function such that If all rows in the matrix with size n, m[0..n-1][0..n-1] are rotations of one another, the function returns true.

  • Step 2 − Define a string produced with the first row's components in it.

  • Step 3 − Next, combine the string to itself to enable the use of this for substring search operations.

  • Step 4 − Now traverse the rows that are remaining

  • Step 5 − Organize the matrix as strings in a vector.

  • Step 6 − Verify whether the present one or the current string is included in the string that was concatenated.

  • Step 7 − Return Yes or No as the result.

Example: C++ Program

Here is the C++ program implementation of the above written algorithm to obtain a C++ Program to Check if all rows of a matrix are circular rotations of each other

#include <bits/stdc++.h>
using namespace std;
const int MAX = 1000;

bool isPermutationMat(int m[MAX][MAX], int n){
   string concatenatedStr = "";
   for (int i = 0; i < n; i++)
      concatenatedStr = concatenatedStr + "-" + to_string(m[0][i]);
   concatenatedStr = concatenatedStr + concatenatedStr;
   for (int i = 1; i < n; i++) {
      string currStr = "";
      for (int j = 0; j < n; j++)
         currStr = currStr + "-" + to_string(m[i][j]);
      if (concatenatedStr.find(currStr) == string::npos)
         return false;
   }
   return true;
}
int main(){
   int n = 4;
   int m[MAX][MAX] = {{5, 6, 7, 8},
      {8, 5, 6, 7},
      {7, 8, 5, 6},
      {8, 7, 4, 5}
   };
   isPermutationMat(m, n) ? cout << "Yes" : cout << "No";
   return 0;
}

Output

No

Conclusion

Likewise, we can C++ Program to Check if all rows of a matrix are circular rotations of each other. The challenge of obtaining the C++ Program to Check if all rows of a matrix are circular rotations of each other is resolved in this article.

Here the C++ programming code as well as the methodology and the algorithm to implement a C++ Program to Check if all rows of a matrix are circular rotations of each other are provided.

Updated on: 30-Oct-2023

34 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements