In: Computer Science
Create a new public static method called ‘isLatinSquare’ that passes in a two-dimensional array. This new method will return ‘true’ if the passed array is a Latin Square. ** SHOULD only be 1 or 2 lines of code
public static boolean isLatinSquare(int[][] matrix) // Public method isLatinSquare where the return type is boolean
{
boolean[][] row_temp = new boolean[matrix.length][matrix.length]; // Two dimensional array declaration
boolean[][] column_temp = new boolean[matrix.length][matrix.length]; // Two dimensional array declaration
for (int i = 0; i < matrix.length; i++) { // Running in loop
if (matrix[i].length != matrix.length) // Condition checking
return false; // Return type
for (int j = 0; j < matrix.length; j++) { // Running in loop
int index = matrix[i][j] - 1; // Performing the operation
if (row_temp[i][index] || column_temp[j][index]) // Condition checking
return false; // Return type
row_temp[i][index] = column_temp[j][index] = true;
}
}
return true; // Return type
}
Hope this helps.