GOLDMANSACHS Coding Question β Solved
Given a matrix of size m * n, where m denotes the number of rows (starting with index 0) and n denotes the number of columns (starting with index 0), the matrix will hold distinct integers as elements.
We need to find the distinct number of positional elements that are either the minimum or maximum in their corresponding row or column. If any row or any column has multiple minimum or maximum elements, return -1.
Example:
Matrix of size 3 * 3:
1 3 5
8 2 7
4 9 6
The expected output is 7.
In this example, we identified the output as 7 based on the following:
- 1: Minimum in both its row and column.
- 4: Maximum in its row.
- 2: Minimum in both its row and column.
- 9: Maximum in both its row and column.
- 8: Maximum in both its row and column.
- 7: Maximum in its column.
- 6: Minimum in its row.
Thus, the total number of distinct elements that are either the minimum or maximum in their respective row or column is 7.
Input:
- m: Integer representing the number of rows.
- n: Integer representing the number of columns.
- matrix: m x n matrix of integers.
Output:
- r: Integer representing the total number of distinct elements which are either the minimum or maximum in their corresponding row or column.
Constraints:
- 0 < m, n < 100
- Elements in the matrix are positive integers.