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 are false, then the matrix is not a Latin square.
Efficient Approach: Here is the more efficient approach using a 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 elements and insert the value of the given element in the 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 the set is less than N or not.
- If Yes, Print “YES”. Otherwise, Print “NO”.
Below is the implementation of the above approach.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Latin Square
- Latin alphabet cipher
- Check whether the number formed by concatenating two numbers is a perfect square or not
- C Program To Check whether Matrix is Skew Symmetric or not
- Check whether the given Matrix is balanced or not
- Check whether a given matrix is orthogonal or not
- Check given matrix is magic square or not
- Check if matrix can be converted to another matrix by transposing square sub-matrices
- Check whether the number can be made perfect square after adding K
- Check if a number is a perfect square having all its digits as a perfect square
- Check if it is possible to make the given matrix increasing matrix or not
- Program to check if a matrix is Binary matrix or not
- Construct a square Matrix whose parity of diagonal sum is same as size of matrix
- Check whether a given array is a k sorted array or not
- Check Whether a number is Duck Number or not
- Program to check whether the given number is Buzz Number or not
- Check whether the given array is perfect or not
- Check whether triangle is valid or not if sides are given
- Check whether a point lies inside a sphere or not
- Check whether the sum of prime elements of the array is prime or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.