Check whether a Matrix is a Latin Square or not
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.
Examples:
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
Naive Approach:
- 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.
// C++ program to check if given matrix // is natural latin square or not #include <bits/stdc++.h> using namespace std; void CheckLatinSquare(int mat[4][4]) { // Size of square matrix is NxN int N = sizeof(mat[0]) / sizeof(mat[0][0]); // Vector of N sets corresponding // to each row. vector<set<int> > rows(N); // Vector of N sets corresponding // to each column. vector<set<int> > cols(N); // Number of invalid elements int invalid = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { rows[i].insert(mat[i][j]); cols[j].insert(mat[i][j]); if (mat[i][j] > N || mat[i][j] <= 0) { invalid++; } } } // Number of rows with // repeatative elements. int numrows = 0; // Number of columns with // repeatative elements. int numcols = 0; // Checking size of every row // and column for (int i = 0; i < N; i++) { if (rows[i].size() != N) { numrows++; } if (cols[i].size() != N) { numcols++; } } if (numcols == 0 && numrows == 0 && invalid == 0) cout << "YES" << endl; else cout << "NO" << endl; return; } // Driver code int main() { int Matrix[4][4] = { { 1, 2, 3, 4 }, { 2, 1, 4, 3 }, { 3, 4, 1, 2 }, { 4, 3, 2, 1 } }; // Funtion call CheckLatinSquare(Matrix); return 0; } |
YES
Got what you were looking for? Learn more and become self sufficient. Start learning Data Sructures & Algorithms with the help of the most trusted DSA Self Paced course, and that too at the most student-friendly price.
Recommended Posts:
- Find any one of the multiple repeating elements in read only array | Set 2
- Subsequence with maximum pairwise absolute difference and minimum size
- Maximize occurrences of values between L and R on sequential addition of Array elements with modulo H
- Sum of all array elements less than X and greater than Y for Q queries
- Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts
- Check if a subsequence of length K with odd sum exists
- Count of all possible bitonic subarrays
- Maximize removal of adjacent array elements based on their absolute value
- Minimize insertions in an array to obtain all sums upto N
- Minimum palindromic subarray removals to make array Empty
- Replace every element with the smallest of all other array elements
- Generate all numbers up to N in Lexicographical Order
- Maximize count of corresponding same elements in given Arrays by Rotation
- Sum of division of the possible pairs for the given Array
- Partition a set into two subsets such that difference between max of one and min of other is minimized
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

