#K88682. Distinct Elements After Row Sorting
Distinct Elements After Row Sorting
Distinct Elements After Row Sorting
You are given a matrix M with R rows and C columns. Your task is to perform the following operation:
- For each row, sort the row in non-decreasing (ascending) order.
After sorting every row, determine the number of distinct integers present in the entire matrix.
Note: Although sorting each row does not change the set of numbers, you must perform the sort on each row as part of the process.
The answer should be computed and printed as a single integer.
The problem can be formally expressed as follows:
Let \(M_{ij}\) be the integer in the i-th row and j-th column. After sorting each row individually, let \(S\) be the set of all integers in the matrix. You are to output \(|S|\), the number of elements in \(S\).
inputFormat
The first line contains two integers R and C denoting the number of rows and columns in the matrix, respectively.
This is followed by R lines, each containing C space-separated integers representing the rows of the matrix.
Input Format:
R C M[0][0] M[0][1] ... M[0][C-1] M[1][0] M[1][1] ... M[1][C-1] ... M[R-1][0] M[R-1][1] ... M[R-1][C-1]
outputFormat
Output a single integer on a new line which is the number of distinct elements in the matrix after sorting each row.
Output Format:
result## sample
3 4
12 5 7 9
2 5 8 9
4 6 10 1
10