Check whether the given matrix is latin square or not
Here given code implementation process.
/*
Java Program for
Check whether the given matrix is latin square or not
*/
import java.util.HashSet;
public class LatinSquare
{
// Display the given matrix element
public void printSquare(int[][] matrix, int r, int c)
{
// Executing the loop through by matrix row
for (int i = 0; i < r ; i++)
{
// Executes the loop through by matrix column
for (int j = 0; j < c ; j++)
{
System.out.print(" "+matrix[i][j]);
}
System.out.print("\n");
}
}
public void isLatinSquare(int[][] matrix, int r, int c)
{
boolean status = true;
if (r == c)
{
// This can collect relevant row and column
HashSet < Integer > row = new HashSet < Integer > ();
HashSet < Integer > col = new HashSet < Integer > ();
// Executing the loop through by matrix row
for (int i = 0; i < r && status; i++)
{
// Executes the loop through by matrix column
for (int j = 0; j < c && status; j++)
{
if (matrix[j][i] > r || matrix[j][i] < 1
|| matrix[i][j] > c || matrix[i][j] < 1)
{
// When the matrix element is outside the matrix size
status = false;
}
else
{
// Add row element
row.add(matrix[j][i]);
// Add col element
col.add(matrix[i][j]);
}
}
if (status && (row.size() != r || col.size() != c))
{
// When result row and column are not N element
status = false;
}
row.clear();
col.clear();
}
}
else
{
// When not square matrix
status = false;
}
printSquare(matrix,r,c);
if (status == true)
{
System.out.println(" Is Latin Square\n");
}
else
{
System.out.println(" Is Not Latin Square\n");
}
}
public static void main(String[] args)
{
LatinSquare task = new LatinSquare();
int[][] matrix1 = {
{
1 , 2 , 3 , 4 , 5
} ,
{
2 , 4 , 1 , 5 , 3
} ,
{
3 , 5 , 4 , 2 , 1
} ,
{
4 , 1 , 5 , 3 , 2
} ,
{
5 , 3 , 2 , 1 , 4
}
};
int[][] matrix2 = {
{
1 , 2 , 4 , 3 , 5
} ,
{
2 , 4 , 1 , 5 , 3
} ,
{
3 , 5 , 4 , 2 , 1
} ,
{
4 , 1 , 5 , 3 , 2
} ,
{
5 , 3 , 2 , 1 , 4
}
};
// Case A
// Get the matrix length
int r = matrix1.length;
int c = matrix1[0].length;
task.isLatinSquare(matrix1, r, c);
// Case B
// Get the matrix length
r = matrix2.length;
c = matrix2[0].length;
task.isLatinSquare(matrix2, r, c);
}
}
input
1 2 3 4 5
2 4 1 5 3
3 5 4 2 1
4 1 5 3 2
5 3 2 1 4
Is Latin Square
1 2 4 3 5
2 4 1 5 3
3 5 4 2 1
4 1 5 3 2
5 3 2 1 4
Is Not Latin Square
// Include header file
#include <iostream>
#include <set>
#define N 5
using namespace std;
class LatinSquare
{
public:
// Display the given matrix element
void printSquare(int matrix[N][N])
{
// Executing the loop through by matrix row
for (int i = 0; i < N; i++)
{
// Executes the loop through by matrix column
for (int j = 0; j < N; j++)
{
cout << " " << matrix[i][j];
}
cout << "\n";
}
}
void isLatinSquare(int matrix[N][N])
{
bool status = true;
if (N > 0)
{
// This can collect relevant row and column
set < int > row ;
set < int > col ;
// Executing the loop through by matrix row
for (int i = 0; i < N && status; i++)
{
// Executes the loop through by matrix column
for (int j = 0; j < N && status; j++)
{
if (matrix[j][i] > N || matrix[j][i] < 1
|| matrix[i][j] > N || matrix[i][j] < 1)
{
// When the matrix element is outside the matrix size
status = false;
}
else
{
// Add row element
row.insert(matrix[j][i]);
// Add col element
col.insert(matrix[i][j]);
}
}
if (status && (row.size() != N || col.size() != N))
{
// When result row and column are not N element
status = false;
}
row.clear();
col.clear();
}
}
else
{
// When not square matrix
status = false;
}
this->printSquare(matrix);
if (status == true)
{
cout << " Is Latin Square\n" << endl;
}
else
{
cout << " Is Not Latin Square\n" << endl;
}
}
};
int main()
{
LatinSquare *task = new LatinSquare();
int matrix1[N][N] = {
{
1 , 2 , 3 , 4 , 5
} , {
2 , 4 , 1 , 5 , 3
} , {
3 , 5 , 4 , 2 , 1
} , {
4 , 1 , 5 , 3 , 2
} , {
5 , 3 , 2 , 1 , 4
}
};
int matrix2[N][N] = {
{
1 , 2 , 4 , 3 , 5
} , {
2 , 4 , 1 , 5 , 3
} , {
3 , 5 , 4 , 2 , 1
} , {
4 , 1 , 5 , 3 , 2
} , {
5 , 3 , 2 , 1 , 4
}
};
// Case A
// Get the matrix length
task->isLatinSquare(matrix1);
// Case B
// Get the matrix length
task->isLatinSquare(matrix2);
return 0;
}
input
1 2 3 4 5
2 4 1 5 3
3 5 4 2 1
4 1 5 3 2
5 3 2 1 4
Is Latin Square
1 2 4 3 5
2 4 1 5 3
3 5 4 2 1
4 1 5 3 2
5 3 2 1 4
Is Not Latin Square
// Include namespace system
using System;
using System.Collections.Generic;
public class LatinSquare
{
// Display the given matrix element
public void printSquare(int[,] matrix, int r, int c)
{
// Executing the loop through by matrix row
for (int i = 0; i < r; i++)
{
// Executes the loop through by matrix column
for (int j = 0; j < c; j++)
{
Console.Write(" " + matrix[i,j]);
}
Console.Write("\n");
}
}
public void isLatinSquare(int[,] matrix, int r, int c)
{
Boolean status = true;
if (r == c)
{
// This can collect relevant row and column
HashSet < int > row = new HashSet < int > ();
HashSet < int > col = new HashSet < int > ();
// Executing the loop through by matrix row
for (int i = 0; i < r && status; i++)
{
// Executes the loop through by matrix column
for (int j = 0; j < c && status; j++)
{
if (matrix[j,i] > r || matrix[j,i] < 1 || matrix[i,j] > c || matrix[i,j] < 1)
{
// When the matrix element is outside the matrix size
status = false;
}
else
{
// Add row element
row.Add(matrix[j,i]);
// Add col element
col.Add(matrix[i,j]);
}
}
if (status && (row.Count != r || col.Count != c))
{
// When result row and column are not N element
status = false;
}
row.Clear();
col.Clear();
}
}
else
{
// When not square matrix
status = false;
}
this.printSquare(matrix, r, c);
if (status == true)
{
Console.WriteLine(" Is Latin Square\n");
}
else
{
Console.WriteLine(" Is Not Latin Square\n");
}
}
public static void Main(String[] args)
{
LatinSquare task = new LatinSquare();
int[,] matrix1 = {
{
1 , 2 , 3 , 4 , 5
} , {
2 , 4 , 1 , 5 , 3
} , {
3 , 5 , 4 , 2 , 1
} , {
4 , 1 , 5 , 3 , 2
} , {
5 , 3 , 2 , 1 , 4
}
};
int[,] matrix2 = {
{
1 , 2 , 4 , 3 , 5
} , {
2 , 4 , 1 , 5 , 3
} , {
3 , 5 , 4 , 2 , 1
} , {
4 , 1 , 5 , 3 , 2
} , {
5 , 3 , 2 , 1 , 4
}
};
// Case A
// Get the matrix length
int r = matrix1.GetLength(0);
int c = matrix1.GetLength(1);
task.isLatinSquare(matrix1, r, c);
// Case B
// Get the matrix length
r = matrix2.GetLength(0);
c = matrix2.GetLength(1);
task.isLatinSquare(matrix2, r, c);
}
}
input
1 2 3 4 5
2 4 1 5 3
3 5 4 2 1
4 1 5 3 2
5 3 2 1 4
Is Latin Square
1 2 4 3 5
2 4 1 5 3
3 5 4 2 1
4 1 5 3 2
5 3 2 1 4
Is Not Latin Square
<?php
/*
Php Program for
Check whether the given matrix is latin square or not
*/
class LatinSquare
{
// Display the given matrix element
public function printSquare($matrix, $r, $c)
{
// Executing the loop through by matrix row
for ($i = 0; $i < $r; $i++)
{
// Executes the loop through by matrix column
for ($j = 0; $j < $c; $j++)
{
echo " ".$matrix[$i][$j];
}
echo "\n";
}
}
public function isLatinSquare($matrix, $r, $c)
{
$status = true;
if ($r == $c)
{
// This can collect relevant row and column
$row = array();
$col = array();
// Executing the loop through by matrix row
for ($i = 0; $i < $r && $status; $i++)
{
// Executes the loop through by matrix column
for ($j = 0; $j < $c && $status; $j++)
{
if ($matrix[$j][$i] > $r || $matrix[$j][$i] < 1
|| $matrix[$i][$j] > $c || $matrix[$i][$j] < 1)
{
// When the matrix element is outside the matrix size
$status = false;
}
else
{
if(!in_array($matrix[$j][$i],$row))
{
// Add row element
$row[] = $matrix[$j][$i];
}
if(!in_array($matrix[$i][$j],$col))
{
// Add col element
$col[] = $matrix[$i][$j];
}
}
}
if ($status && (count($row) != $r || count($col) != $c))
{
// When result row and column are not N element
$status = false;
};
unset($row);
unset($col);
$row = array();
$col = array();
}
}
else
{
// When not square matrix
$status = false;
}
$this->printSquare($matrix, $r, $c);
if ($status == true)
{
echo " Is Latin Square\n".
"\n";
}
else
{
echo " Is Not Latin Square\n".
"\n";
}
}
}
function main()
{
$task = new LatinSquare();
$matrix1 = array(array(1, 2, 3, 4, 5), array(2, 4, 1, 5, 3), array(3, 5, 4, 2, 1), array(4, 1, 5, 3, 2), array(5, 3, 2, 1, 4));
$matrix2 = array(array(1, 2, 4, 3, 5), array(2, 4, 1, 5, 3), array(3, 5, 4, 2, 1), array(4, 1, 5, 3, 2), array(5, 3, 2, 1, 4));
// Case A
// Get the matrix length
$r = count($matrix1);
$c = count($matrix1[0]);
$task->isLatinSquare($matrix1, $r, $c);
// Case B
// Get the matrix length
$r = count($matrix2);
$c = count($matrix2[0]);
$task->isLatinSquare($matrix2, $r, $c);
}
main();
input
1 2 3 4 5
2 4 1 5 3
3 5 4 2 1
4 1 5 3 2
5 3 2 1 4
Is Latin Square
1 2 4 3 5
2 4 1 5 3
3 5 4 2 1
4 1 5 3 2
5 3 2 1 4
Is Not Latin Square
/*
Node JS Program for
Check whether the given matrix is latin square or not
*/
class LatinSquare
{
// Display the given matrix element
printSquare(matrix, r, c)
{
// Executing the loop through by matrix row
for (var i = 0; i < r; i++)
{
// Executes the loop through by matrix column
for (var j = 0; j < c; j++)
{
process.stdout.write(" " + matrix[i][j]);
}
process.stdout.write("\n");
}
}
isLatinSquare(matrix, r, c)
{
var status = true;
if (r == c)
{
// This can collect relevant row and column
var row = new Set();
var col = new Set();
// Executing the loop through by matrix row
for (var i = 0; i < r && status; i++)
{
// Executes the loop through by matrix column
for (var j = 0; j < c && status; j++)
{
if (matrix[j][i] > r || matrix[j][i] < 1
|| matrix[i][j] > c || matrix[i][j] < 1)
{
// When the matrix element is outside the matrix size
status = false;
}
else
{
// Add row element
row.add(matrix[j][i]);
// Add col element
col.add(matrix[i][j]);
}
}
if (status && (row.size != r || col.size != c))
{
// When result row and column are not N element
status = false;
}
row.clear();
col.clear();
}
}
else
{
// When not square matrix
status = false;
}
this.printSquare(matrix, r, c);
if (status == true)
{
console.log(" Is Latin Square\n");
}
else
{
console.log(" Is Not Latin Square\n");
}
}
}
function main()
{
var task = new LatinSquare();
var matrix1 = [
[1, 2, 3, 4, 5] ,
[2, 4, 1, 5, 3] ,
[3, 5, 4, 2, 1] ,
[4, 1, 5, 3, 2] ,
[5, 3, 2, 1, 4]
];
var matrix2 = [
[1, 2, 4, 3, 5] ,
[2, 4, 1, 5, 3] ,
[3, 5, 4, 2, 1] ,
[4, 1, 5, 3, 2] ,
[5, 3, 2, 1, 4]
];
// Case A
// Get the matrix length
var r = matrix1.length;
var c = matrix1[0].length;
task.isLatinSquare(matrix1, r, c);
// Case B
// Get the matrix length
r = matrix2.length;
c = matrix2[0].length;
task.isLatinSquare(matrix2, r, c);
}
main();
input
1 2 3 4 5
2 4 1 5 3
3 5 4 2 1
4 1 5 3 2
5 3 2 1 4
Is Latin Square
1 2 4 3 5
2 4 1 5 3
3 5 4 2 1
4 1 5 3 2
5 3 2 1 4
Is Not Latin Square
# Python 3 Program for
# Check whether the given matrix is latin square or not
class LatinSquare :
# Display the given matrix element
def printSquare(self, matrix, r, c) :
i = 0
j = 0
# Executing the loop through by matrix row
while (i < r) :
# Executes the loop through by matrix column
while (j < c) :
print(" ", matrix[i][j], end = "")
j += 1
print(end = "\n")
i += 1
j = 0
def isLatinSquare(self, matrix, r, c) :
status = True
if (r == c) :
row = set()
col = set()
# Executing the loop through by matrix row
i = 0
while (i < r and status) :
# Executes the loop through by matrix column
j = 0
while (j < c and status) :
if (matrix[j][i] > r or matrix[j][i] < 1 or matrix[i][j] > c or matrix[i][j] < 1) :
# When the matrix element is outside the matrix size
status = False
else :
# Add row element
row.add(matrix[j][i])
# Add col element
col.add(matrix[i][j])
j += 1
if (status and(len(row) != r or len(col) != c)) :
# When result row and column are not N element
status = False
row.clear()
col.clear()
i += 1
else :
# When not square matrix
status = False
self.printSquare(matrix, r, c)
if (status == True) :
print(" Is Latin Square\n")
else :
print(" Is Not Latin Square\n")
def main() :
task = LatinSquare()
matrix1 = [
[1, 2, 3, 4, 5] , [2, 4, 1, 5, 3] , [3, 5, 4, 2, 1] , [4, 1, 5, 3, 2] , [5, 3, 2, 1, 4]
]
matrix2 = [
[1, 2, 4, 3, 5] , [2, 4, 1, 5, 3] , [3, 5, 4, 2, 1] , [4, 1, 5, 3, 2] , [5, 3, 2, 1, 4]
]
r = len(matrix1)
c = len(matrix1[0])
task.isLatinSquare(matrix1, r, c)
# Case B
# Get the matrix length
r = len(matrix2)
c = len(matrix2[0])
task.isLatinSquare(matrix2, r, c)
if __name__ == "__main__": main()
input
1 2 3 4 5
2 4 1 5 3
3 5 4 2 1
4 1 5 3 2
5 3 2 1 4
Is Latin Square
1 2 4 3 5
2 4 1 5 3
3 5 4 2 1
4 1 5 3 2
5 3 2 1 4
Is Not Latin Square
require 'set'
# Ruby Program for
# Check whether the given matrix is latin square or not
class LatinSquare
# Display the given matrix element
def printSquare(matrix, r, c)
i = 0
j = 0
# Executing the loop through by matrix row
while (i < r)
# Executes the loop through by matrix column
while (j < c)
print(" ", matrix[i][j])
j += 1
end
print("\n")
i += 1
j = 0
end
end
def isLatinSquare(matrix, r, c)
status = true
if (r == c)
# This can collect relevant row and column
row = SortedSet.new()
col = SortedSet.new()
# Executing the loop through by matrix row
i = 0
while (i < r && status)
# Executes the loop through by matrix column
j = 0
while (j < c && status)
if (matrix[j][i] > r || matrix[j][i] < 1 ||
matrix[i][j] > c || matrix[i][j] < 1)
# When the matrix element is outside the matrix size
status = false
else
# Add row element
row.add(matrix[j][i])
# Add col element
col.add(matrix[i][j])
end
j += 1
end
if (status && (row.size() != r || col.size() != c))
# When result row and column are not N element
status = false
end
row.clear()
col.clear()
i += 1
end
else
# When not square matrix
status = false
end
self.printSquare(matrix, r, c)
if (status == true)
print(" Is Latin Square\n", "\n")
else
print(" Is Not Latin Square\n", "\n")
end
end
end
def main()
task = LatinSquare.new()
matrix1 = [
[1, 2, 3, 4, 5] ,
[2, 4, 1, 5, 3] ,
[3, 5, 4, 2, 1] ,
[4, 1, 5, 3, 2] ,
[5, 3, 2, 1, 4]
]
matrix2 = [
[1, 2, 4, 3, 5] ,
[2, 4, 1, 5, 3] ,
[3, 5, 4, 2, 1] ,
[4, 1, 5, 3, 2] ,
[5, 3, 2, 1, 4]
]
# Case A
# Get the matrix length
r = matrix1.length
c = matrix1[0].length
task.isLatinSquare(matrix1, r, c)
# Case B
# Get the matrix length
r = matrix2.length
c = matrix2[0].length
task.isLatinSquare(matrix2, r, c)
end
main()
input
1 2 3 4 5
2 4 1 5 3
3 5 4 2 1
4 1 5 3 2
5 3 2 1 4
Is Latin Square
1 2 4 3 5
2 4 1 5 3
3 5 4 2 1
4 1 5 3 2
5 3 2 1 4
Is Not Latin Square
import scala.collection.mutable._;
/*
Scala Program for
Check whether the given matrix is latin square or not
*/
class LatinSquare()
{
// Display the given matrix element
def printSquare(matrix: Array[Array[Int]], r: Int, c: Int): Unit = {
var i: Int = 0;
var j: Int = 0;
// Executing the loop through by matrix row
while (i < r)
{
// Executes the loop through by matrix column
while (j < c)
{
print(" " + matrix(i)(j));
j += 1;
}
print("\n");
i += 1;
j = 0;
}
}
def isLatinSquare(matrix: Array[Array[Int]], r: Int, c: Int): Unit = {
var status: Boolean = true;
if (r == c)
{
// This can collect relevant row and column
var row: Set[Int] = Set();
var col: Set[Int] = Set();
// Executing the loop through by matrix row
var i: Int = 0;
while (i < r && status)
{
// Executes the loop through by matrix column
var j: Int = 0;
while (j < c && status)
{
if (matrix(j)(i) > r || matrix(j)(i) < 1 || matrix(i)(j) > c || matrix(i)(j) < 1)
{
// When the matrix element is outside the matrix size
status = false;
}
else
{
// Add row element
row.add(matrix(j)(i));
// Add col element
col.add(matrix(i)(j));
}
j += 1;
}
if (status && (row.size != r || col.size != c))
{
// When result row and column are not N element
status = false;
}
row.clear();
col.clear();
i += 1;
}
}
else
{
// When not square matrix
status = false;
}
printSquare(matrix, r, c);
if (status == true)
{
println(" Is Latin Square\n");
}
else
{
println(" Is Not Latin Square\n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: LatinSquare = new LatinSquare();
var matrix1: Array[Array[Int]] = Array(
Array(1, 2, 3, 4, 5),
Array(2, 4, 1, 5, 3),
Array(3, 5, 4, 2, 1),
Array(4, 1, 5, 3, 2),
Array(5, 3, 2, 1, 4)
);
var matrix2: Array[Array[Int]] = Array(
Array(1, 2, 4, 3, 5),
Array(2, 4, 1, 5, 3),
Array(3, 5, 4, 2, 1),
Array(4, 1, 5, 3, 2),
Array(5, 3, 2, 1, 4)
);
// Case A
// Get the matrix length
var r: Int = matrix1.length;
var c: Int = matrix1(0).length;
task.isLatinSquare(matrix1, r, c);
// Case B
// Get the matrix length
r = matrix2.length;
c = matrix2(0).length;
task.isLatinSquare(matrix2, r, c);
}
}
input
1 2 3 4 5
2 4 1 5 3
3 5 4 2 1
4 1 5 3 2
5 3 2 1 4
Is Latin Square
1 2 4 3 5
2 4 1 5 3
3 5 4 2 1
4 1 5 3 2
5 3 2 1 4
Is Not Latin Square
/*
Swift 4 Program for
Check whether the given matrix is latin square or not
*/
class LatinSquare
{
// Display the given matrix element
func printSquare(_ matrix: [
[Int]
], _ r: Int, _ c: Int)
{
var i: Int = 0;
var j: Int = 0;
// Executing the loop through by matrix row
while (i < r)
{
// Executes the loop through by matrix column
while (j < c)
{
print(" ", matrix[i][j], terminator: "");
j += 1;
}
print(terminator: "\n");
i += 1;
j = 0;
}
}
func isLatinSquare(_ matrix: [
[Int]
], _ r: Int, _ c: Int)
{
var status: Bool = true;
if (r == c)
{
// This can collect relevant row and column
var row = Set<Int>();
var col = Set<Int>();
// Executing the loop through by matrix row
var i: Int = 0;
while (i < r && status)
{
// Executes the loop through by matrix column
var j: Int = 0;
while (j < c && status)
{
if (matrix[j][i] > r || matrix[j][i] < 1 ||
matrix[i][j] > c || matrix[i][j] < 1)
{
// When the matrix element is outside the matrix size
status = false;
}
else
{
// Add row element
row.insert(matrix[j][i]);
// Add col element
col.insert(matrix[i][j]);
}
j += 1;
}
if (status && (row.count != r || col.count != c))
{
// When result row and column are not N element
status = false;
}
row.removeAll();
col.removeAll();
i += 1;
}
}
else
{
// When not square matrix
status = false;
}
self.printSquare(matrix, r, c);
if (status == true)
{
print(" Is Latin Square\n");
}
else
{
print(" Is Not Latin Square\n");
}
}
}
func main()
{
let task: LatinSquare = LatinSquare();
let matrix1: [[Int]] = [
[1, 2, 3, 4, 5] ,
[2, 4, 1, 5, 3] ,
[3, 5, 4, 2, 1] ,
[4, 1, 5, 3, 2] ,
[5, 3, 2, 1, 4]
];
let matrix2: [[Int]] = [
[1, 2, 4, 3, 5] ,
[2, 4, 1, 5, 3] ,
[3, 5, 4, 2, 1] ,
[4, 1, 5, 3, 2] ,
[5, 3, 2, 1, 4]
];
// Case A
// Get the matrix length
var r: Int = matrix1.count;
var c: Int = matrix1[0].count;
task.isLatinSquare(matrix1, r, c);
// Case B
// Get the matrix length
r = matrix2.count;
c = matrix2[0].count;
task.isLatinSquare(matrix2, r, c);
}
main();
input
1 2 3 4 5
2 4 1 5 3
3 5 4 2 1
4 1 5 3 2
5 3 2 1 4
Is Latin Square
1 2 4 3 5
2 4 1 5 3
3 5 4 2 1
4 1 5 3 2
5 3 2 1 4
Is Not Latin Square
/*
Kotlin Program for
Check whether the given matrix is latin square or not
*/
class LatinSquare
{
// Display the given matrix element
fun printSquare(matrix: Array < Array < Int >> , r: Int, c: Int): Unit
{
var i: Int = 0;
var j: Int = 0;
while (i < r)
{
while (j < c)
{
print(" " + matrix[i][j]);
j += 1;
}
print("\n");
i += 1;
j = 0;
}
}
fun isLatinSquare(matrix: Array < Array < Int >> , r: Int, c: Int): Unit
{
var status: Boolean = true;
if (r == c)
{
// This can collect relevant row and column
val row: MutableSet <Int> = mutableSetOf <Int> ();
val col: MutableSet <Int> = mutableSetOf <Int> ();
var i: Int = 0;
while (i < r && status)
{
var j: Int = 0;
while (j < c && status)
{
if (matrix[j][i] > r || matrix[j][i] < 1
|| matrix[i][j] > c || matrix[i][j] < 1)
{
// When the matrix element is outside the matrix size
status = false;
}
else
{
// Add row element
row.add(matrix[j][i]);
// Add col element
col.add(matrix[i][j]);
}
j += 1;
}
if (status && (row.count() != r || col.count() != c))
{
// When result row and column are not N element
status = false;
}
row.clear();
col.clear();
i += 1;
}
}
else
{
// When not square matrix
status = false;
}
this.printSquare(matrix, r, c);
if (status == true)
{
println(" Is Latin Square\n");
}
else
{
println(" Is Not Latin Square\n");
}
}
}
fun main(args: Array < String > ): Unit
{
val task: LatinSquare = LatinSquare();
val matrix1: Array < Array < Int >> = arrayOf(
arrayOf(1, 2, 3, 4, 5),
arrayOf(2, 4, 1, 5, 3),
arrayOf(3, 5, 4, 2, 1),
arrayOf(4, 1, 5, 3, 2),
arrayOf(5, 3, 2, 1, 4)
);
val matrix2: Array < Array < Int >> = arrayOf(
arrayOf(1, 2, 4, 3, 5),
arrayOf(2, 4, 1, 5, 3),
arrayOf(3, 5, 4, 2, 1),
arrayOf(4, 1, 5, 3, 2),
arrayOf(5, 3, 2, 1, 4)
);
// Case A
// Get the matrix length
var r: Int = matrix1.count();
var c: Int = matrix1[0].count();
task.isLatinSquare(matrix1, r, c);
// Case B
// Get the matrix length
r = matrix2.count();
c = matrix2[0].count();
task.isLatinSquare(matrix2, r, c);
}
input
1 2 3 4 5
2 4 1 5 3
3 5 4 2 1
4 1 5 3 2
5 3 2 1 4
Is Latin Square
1 2 4 3 5
2 4 1 5 3
3 5 4 2 1
4 1 5 3 2
5 3 2 1 4
Is Not Latin Square
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment