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 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.
C++
// 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 // repetitive elements. int numrows = 0; // Number of columns with // repetitive 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 codeint main(){ int Matrix[4][4] = { { 1, 2, 3, 4 }, { 2, 1, 4, 3 }, { 3, 4, 1, 2 }, { 4, 3, 2, 1 } }; // Function call CheckLatinSquare(Matrix); return 0;} |
Java
// Java program to check if given matrix// is natural latin square or notimport java.util.*;class GFG{ @SuppressWarnings("unchecked")static void CheckLatinSquare(int mat[][]){ // Size of square matrix is NxN int N = mat.length; // Vector of N sets corresponding // to each row. HashSet<Integer>[] rows = new HashSet[N]; // Vector of N sets corresponding // to each column. HashSet<Integer>[] cols = new HashSet[N]; for(int i = 0; i < N; i++) { rows[i] = new HashSet<Integer>(); cols[i] = new HashSet<Integer>(); } // Number of invalid elements int invalid = 0; for(int i = 0; i < N; i++) { for(int j = 0; j < N; j++) { rows[i].add(mat[i][j]); cols[j].add(mat[i][j]); if (mat[i][j] > N || mat[i][j] <= 0) { invalid++; } } } // Number of rows with // repetitive elements. int numrows = 0; // Number of columns with // repetitive 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) System.out.print("YES" + "\n"); else System.out.print("NO" + "\n"); return;} // Driver codepublic static void main(String[] args){ int Matrix[][] = { { 1, 2, 3, 4 }, { 2, 1, 4, 3 }, { 3, 4, 1, 2 }, { 4, 3, 2, 1 } }; // Function call CheckLatinSquare(Matrix);}}// This code is contributed by 29AjayKumar |
Python3
# Python3 program to check if given matrix# is natural latin square or notdef CheckLatinSquare(mat): # Size of square matrix is NxN N = len(mat) # Vector of N sets corresponding # to each row. rows = [] for i in range(N): rows.append(set([])) # Vector of N sets corresponding # to each column. cols = [] for i in range(N): cols.append(set([])) # Number of invalid elements invalid = 0 for i in range(N): for j in range(N): rows[i].add(mat[i][j]) cols[j].add(mat[i][j]) if (mat[i][j] > N or mat[i][j] <= 0) : invalid += 1 # Number of rows with # repetitive elements. numrows = 0 # Number of columns with # repetitive elements. numcols = 0 # Checking size of every row # and column for i in range(N): if (len(rows[i]) != N) : numrows+=1 if (len(cols[i]) != N) : numcols+=1 if (numcols == 0 or numrows == 0 and invalid == 0) : print("YES") else: print("NO") returnMatrix = [ [ 1, 2, 3, 4 ], [ 2, 1, 4, 3 ], [ 3, 4, 1, 2 ], [ 4, 3, 2, 1 ] ] # Function callCheckLatinSquare(Matrix)# This code is contributed by decode2207. |
C#
// C# program to check if given matrix// is natural latin square or notusing System;using System.Collections.Generic;class GFG{ static void CheckLatinSquare(int[, ] mat) { // Size of square matrix is NxN int N = mat.GetLength(0); // List of N sets corresponding // to each row. HashSet<int>[] rows = new HashSet<int>[ N ]; // List of N sets corresponding // to each column. HashSet<int>[] cols = new HashSet<int>[ N ]; for (int i = 0; i < N; i++) { rows[i] = new HashSet<int>(); cols[i] = new HashSet<int>(); } // Number of invalid elements int invalid = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { rows[i].Add(mat[i, j]); cols[j].Add(mat[i, j]); if (mat[i, j] > N || mat[i, j] <= 0) { invalid++; } } } // Number of rows with // repetitive elements. int numrows = 0; // Number of columns with // repetitive elements. int numcols = 0; // Checking size of every row // and column for (int i = 0; i < N; i++) { if (rows[i].Count != N) { numrows++; } if (cols[i].Count != N) { numcols++; } } if (numcols == 0 && numrows == 0 && invalid == 0) Console.Write("YES" + "\n"); else Console.Write("NO" + "\n"); return; } // Driver code public static void Main(String[] args) { int[, ] Matrix = {{1, 2, 3, 4}, {2, 1, 4, 3}, {3, 4, 1, 2}, {4, 3, 2, 1}}; // Function call CheckLatinSquare(Matrix); }}// This code is contributed by Amit Katiyar |
Javascript
<script>// javascript program to check if given matrix// is natural latin square or notfunction CheckLatinSquare(mat){ // Size of square matrix is NxN var N = mat.length; // Vector of N sets corresponding // to each row. var rows = new Array(N); // Vector of N sets corresponding // to each column. var cols = new Array(N); for(var i = 0; i < N; i++) { rows[i] = new Set(); cols[i] = new Set(); } // Number of invalid elements var invalid = 0; for(var i = 0; i < N; i++) { for(var j = 0; j < N; j++) { rows[i].add(mat[i][j]); cols[j].add(mat[i][j]); if (mat[i][j] > N || mat[i][j] <= 0) { invalid++; } } } // Number of rows with // repetitive elements. var numrows = 0; // Number of columns with // repetitive elements. var numcols = 0; // Checking size of every row // and column for(var i = 0; i < N; i++) { if (rows[i].size != N) { numrows++; } if (cols[i].size != N) { numcols++; } } if (numcols == 0 && numrows == 0 && invalid == 0) document.write("YES"); else document.write("NO"); return;} // Driver codevar Matrix = [ [ 1, 2, 3, 4 ], [ 2, 1, 4, 3 ], [ 3, 4, 1, 2 ], [ 4, 3, 2, 1 ] ]; // Function call CheckLatinSquare(Matrix);// This code is contributed by 29AjayKumar </script> |
YES
Time Complexity: O(N*N)
Auxiliary Space: O(N*N)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
