Latin Squares
InterviewTime limit1sMemory limit512 MB
Read an n by n grid of base-n digit characters and report whether it is a Latin square, and if so whether it is in reduced form.
- Level
Easy2 of 10
- Topics
- Implementation, Array, Simulation, Hash map
- Solved
- No attempts yet
Problem
A Latin square is an array filled with different digits, where every digit occurs exactly once in each row and exactly once in each column. (The name comes from Leonhard Euler, who used Latin characters in his papers on the subject.)
A Latin square is in reduced form when its top row and its leftmost column are both in natural order. The natural order of a set of digits is by increasing value.
Read an array, decide whether it is a Latin square, and if it is, decide whether it is in reduced form.
Input
The first line contains a single integer (). Each of the next lines contains digits in base . Digit values below 10 are written with the characters 0 through 9, and values from 10 to 35 are written with the uppercase letters A through Z. Every character in the input is legal in base . For example, if is 3, the only characters in the lines describing the array are 0, 1, and 2.
Output
If the given array is not a Latin square, print No on a single line. If it is a Latin square but not in reduced form, print Not Reduced. If it is a Latin square in reduced form, print Reduced.