9565. Minimum among maximums

 

A two-dimensional array of size n * m is given. Find the maximum element in each row of the array. Then, among all the maximum elements, select the minimum.

 

Input. The first line contains two integers n and m – the number of rows and columns of the array, respectively. The next n lines contain m integers each. All input numbers do not exceed 100 in absolute value.

 

Output. Print a single integer – the minimum among the maximum elements of the rows.

 

Sample input

Sample output

4 5

1 5 3 2 4

2 4 3 2 2

6 7 8 9 5

8 9 4 2 5

4

 

 

SOLUTION

two-dimensional array

 

Algorithm analysis

For each row of the array, find its maximum element. Let res[i] store the maximum element of the i-th row. Then, find the minimum element in the array res.

 

Example

Let’s consider the given example. Next to each row, write down its largest element. Highlight the smallest among these largest elements.

 

Algorithm implementation

Declare the arrays.

 

int a[101][101], res[101];

 

Read the sizes of the array, n and m.

 

scanf("%d %d", &n, &m);

 

Since res[i] stores the maximum element of the i-th row (1 ≤ in), initialize it with the smallest possible value.

 

for (j = 1; j <= n; j++)

  res[j] = -101;

 

Iterate through the rows of the matrix.

 

for (i = 1; i <= n; i++)

 

Read the elements of the i-th row and find its maximum value, storing it in res[i].

 

  for (j = 1; j <= m; j++)

  {

    scanf("%d", &a[i][j]);

    if (a[i][j] > res[i]) res[i] = a[i][j];

  }

 

Find the smallest element mn in the array res.

 

mn = 101;

for (i = 1; i <= n; i++)

  if (res[i] < mn) mn = res[i];

 

Print the answer.

 

printf("%d\n", mn);

 

Python implementation

Read the sizes of the array, n and m.

 

n, m = map(int, input().split())

 

Initialize the list res.

 

res = []

 

Iterate through the rows of the matrix.

 

for i in range(n):

 

Read the elements of the current row of the matrix and find its maximum value, which is then added to the end of the list res.

 

  row = list(map(int, input().split()))

  res.append(max(row))

 

Find the smallest element ans in the array res and print it.

 

ans = min(res)

print(ans)