See the original problem on HackerRank.
We need to check if any row, columns or sub-grid contains duplicates. We can use an array of booleans (or a frequency array) for each type of component to check.
Here is the idea:
Basically, we read a coordinate and we check if it has been already allocated in any of the component.
This problem can be used to think about the design. For example:
- what if multiple occurrences are allowed? (maybe boolean arrays will be replaced with frequency tables)
- what if the grid dimensions are not statically known?
- what if the grid shape is different (N-dimensional)?
- what if the check requirements can change dynamically?