Latin Square
Last Updated :
11 Sep, 2023
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.
Examples :
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 is stored in a Latin Square?
- In the first row, the numbers are stored from 1 to n serially.
- In 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 the same way for the remaining rows.
Note: There may be more than one possible configuration of a n x n Latin square.
Implementation:
C++
#include <iostream>
using namespace std;
void printLatin( int n)
{
int k = n+1;
for ( int i=1; i<=n; i++)
{
int temp = k;
while (temp <= n)
{
cout << temp << " " ;
temp++;
}
for ( int j=1; j<k; j++)
cout << j << " " ;
k--;
cout << endl;
}
}
int main( void )
{
int n = 5;
printLatin(n);
return 0;
}
|
C
#include<stdio.h>
void printLatin( int n)
{
int k = n+1;
for ( int i=1; i<=n; i++)
{
int temp = k;
while (temp <= n)
{
printf ( "%d " , temp);
temp++;
}
for ( int j=1; j<k; j++)
printf ( "%d " , j);
k--;
printf ( "\n" );
}
}
int main( void )
{
int n = 5;
printLatin(n);
return 0;
}
|
Java
class GFG {
static void printLatin( int n)
{
int k = n+ 1 ;
for ( int i = 1 ; i <= n; i++)
{
int temp = k;
while (temp <= n)
{
System.out.print(temp + " " );
temp++;
}
for ( int j = 1 ; j < k; j++)
System.out.print(j + " " );
k--;
System.out.println();
}
}
public static void main (String[] args)
{
int n = 5 ;
printLatin(n);
}
}
|
Python 3
def printLatin(n):
k = n + 1
for i in range ( 1 , n + 1 , 1 ):
temp = k
while (temp < = n) :
print (temp, end = " " )
temp + = 1
for j in range ( 1 , k):
print (j, end = " " )
k - = 1
print ()
n = 5
printLatin(n)
|
C#
using System;
class GFG {
static void printLatin( int n)
{
int k = n + 1;
for ( int i = 1; i <= n; i++)
{
int temp = k;
while (temp <= n)
{
Console.Write(temp + " " );
temp++;
}
for ( int j = 1; j < k; j++)
Console.Write(j + " " );
k--;
Console.WriteLine();
}
}
public static void Main ()
{
int n = 5;
printLatin(n);
}
}
|
PHP
<?php
function printLatin( $n )
{
$k = $n + 1;
for ( $i = 1; $i <= $n ; $i ++)
{
$temp = $k ;
while ( $temp <= $n )
{
echo $temp , " " ;
$temp ++;
}
for ( $j = 1; $j < $k ; $j ++)
echo $j , " " ;
$k --;
echo "\n" ;
}
}
$n = 5;
printLatin( $n );
?>
|
Javascript
<script>
function printLatin(n)
{
let k = n + 1;
for (let i = 1; i <= n; i++)
{
let temp = k;
while (temp <= n)
{
document.write(temp + " " );
temp++;
}
for (let j = 1; j < k; j++)
document.write(j + " " );
k--;
document.write( "</br>" );
}
}
let n = 5;
printLatin(n);
</script>
|
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
Time Complexity: O(n*n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...