A Latin Square is a n x n grid filled by n distinct numbers each appearing exactly once in each row and column. Given an input n, we have to print a n x n matrix consisting of numbers from 1 to n each appearing exactly once in each row and each column.
Input: 3 Output: 1 2 3 3 1 2 2 3 1 Input: 5 Output: 1 2 3 4 5 5 1 2 3 4 4 5 1 2 3 3 4 5 1 2 2 3 4 5 1
Did you find any pattern in which the number are stored in a Latin Square?
- In the first row, the numbers are stored from 1 to n serially.
- the second row, the numbers are shifted to the right by one column. i.e, 1 is stored at 2nd column now and so on.
- In the third row, the numbers are shifted to the right by two columns. i.e, 1 is stored at 3rd column now and so on.
- We continue same way for remaining rows.
Note: There may be more than one possible configuration of a n x n latin square.
1 2 3 4 5 5 1 2 3 4 4 5 1 2 3 3 4 5 1 2 2 3 4 5 1
This article is contributed by Pratik Agarwal. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Check whether a Matrix is a Latin Square or not
- Magic Square
- Magic Square | Even Order
- Number of square matrices with all 1s
- Direction at last square block
- Maximum size square sub-matrix with all 1s
- How to access elements of a Square Matrix
- Sum of Area of all possible square inside a rectangle
- Sum of non-diagonal parts of a square Matrix
- Check given matrix is magic square or not
- Maximum and Minimum in a square matrix.
- Maximum perimeter of a square in a 2D grid
- Product of middle row and column in an odd square matrix
- Count all square sub-matrix with sum greater than the given number S
- Find the product of sum of two diagonals of a square Matrix