Given a square matrix of size N x N, the task is to check if it is Latin square or not.
A square matrix is a Latin Square if each cell of the matrix contains one of N different values (in the range [1, N]), and no value is repeated within a row or a column.
Input: 1 2 3 4 2 1 4 3 3 4 1 2 4 3 2 1 Output: YES Input: 2 2 2 2 2 3 2 3 2 2 2 3 2 2 2 2 Output: NO
- For every element, we first check whether the given element is already present in the given row and given column by iterating over all the elements of the given row and given column.
- If not, then check whether the value is less than or equal to N, if yes, move for the next element.
- If any of the above points is false, then the matrix is not a Latin square.
Efficient Approach: Here is the more efficient approach using Set data structure in C++:
- Define sets for each row and each column and create two arrays of sets, one for all the rows and the other for columns.
- Iterate over all the element and insert the value of the given element in corresponding row set and in the corresponding column set.
- Also, check whether the given value is less than N or not. If not, Print “NO” and return.
- Now, Iterate over all row sets and column sets and check if the size of set is less than N or not.
- If Yes, Print “YES”. Otherwise Print “NO”.
Below is the implementation of the above approach.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Latin Square
- Check if matrix can be converted to another matrix by transposing square sub-matrices
- Check given matrix is magic square or not
- Check if a matrix contains a square submatrix with 0 as boundary element
- Maximum and Minimum in a square matrix.
- Maximum size square sub-matrix with all 1s
- Sum of non-diagonal parts of a square Matrix
- How to access elements of a Square Matrix
- Check if a given matrix can be converted to another given matrix by row and column exchanges
- Program to check diagonal matrix and scalar matrix
- Find a Square Matrix such that sum of elements in every row and column is K
- Print all the sub diagonal elements of the given square matrix
- Sum of both diagonals of a spiral odd-order square matrix
- Find the product of sum of two diagonals of a square Matrix
- Count all square sub-matrix with sum greater than the given number S
- Maximum size square Sub-Matrix with sum less than or equals to K
- Inplace rotate square matrix by 90 degrees | Set 1
- Print maximum sum square sub-matrix of given size
- Sum of all parts of a square Matrix divided by its diagonals
- Given an n x n square matrix, find sum of all sub-squares of size k x k
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.