#C10453. Most Frequent Color in a Grid
Most Frequent Color in a Grid
Most Frequent Color in a Grid
You are given a grid of size \(N \times N\) initially filled with zeros. You will also receive \(M\) instructions, where each instruction is of the form \((R, C, K)\). For each instruction, you must update the cell at row \(R\) and column \(C\) with the color code \(K\). After processing all instructions, determine the color code that appears most frequently in the grid, excluding zeros. If no non-zero color appears or if the grid contains only zeros, output \(\infty\) (represented as "inf" in the output). In case multiple color codes share the same maximum frequency, choose the smallest color code among them.
Note: The grid is 0-indexed. All input and output should be handled via standard input and standard output.
inputFormat
The first line contains two integers \(N\) and \(M\) representing the size of the grid and the number of instructions, respectively.
The next \(M\) lines each contain three integers \(R\), \(C\), and \(K\), describing an instruction to color the cell at row \(R\) and column \(C\) with the color code \(K\).
outputFormat
Output a single line. If there is at least one non-zero color in the grid, print the color code which appears most frequently. If there are multiple such color codes, print the smallest one. If no non-zero colors appear in the grid, output "inf".
## sample5 8
0 0 1
0 1 2
0 2 2
1 1 3
2 2 1
2 3 3
3 3 1
4 4 3
1