Maximize minimum of array generated by maximums of same indexed elements of two rows of a given Matrix
Last Updated :
08 Mar, 2022
Given a matrix mat[][] of N rows and M columns, the task is to choose any two rows(i, j) (0 ? i, j ? N – 1) and construct a new array A[] of size M, where A[k] = max(mat[i][k], mat[j][k]) such that minimum of A[] is maximum possible.
Examples:
Input: mat[][] = {{5, 0, 3, 1, 2}, {1, 8, 9, 1, 3}, {1, 2, 3, 4, 5}, {9, 1, 0, 3, 7}, {2, 3, 0, 6, 3}, {6, 4, 1, 7, 0}}
Output: 3
Explanation:
Choose two rows i = 0 and j = 4. Then, A[] = {5, 3, 3, 6, 3} and min(A[]) is 3, which is maximum possible.
Input: mat[][] = {{2, 13, 41, 5}, {91, 11, 10, 13}, {12, 3, 28, 6}}
Output: 13
Explanation:
Choose two rows i = 0 and j = 1. Then, A[] = {91, 13, 41, 13} and min(A[]) is 13, which is maximum possible.
Approach: Follow the steps below to solve the problem:
- Initialize a variable global_max with INT_MAX, which stores the maximum of a minimum of array constructed from any two rows of mat[][].
- Iterate through every possible combination of rows i and j, and find the minimum of array constructed from them as row_min.
- In each iteration, update global_max to the maximum of row_min and itself.
- After the above steps, print the value of global_max as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getMaximum( int N, int M,
vector<vector< int > > mat)
{
int global_max = INT_MIN;
for ( int i = 0; i < N; i++) {
for ( int j = i + 1; j < N; j++) {
int row_min = INT_MAX;
for ( int k = 0; k < M; k++) {
int m = max(mat[i][k],
mat[j][k]);
row_min = min(row_min, m);
}
global_max = max(global_max,
row_min);
}
}
return global_max;
}
int main()
{
vector<vector< int > > mat
= { { 5, 0, 3, 1, 2 },
{ 1, 8, 9, 1, 3 },
{ 1, 2, 3, 4, 5 },
{ 9, 1, 0, 3, 7 },
{ 2, 3, 0, 6, 3 },
{ 6, 4, 1, 7, 0 } };
int N = 6, M = 5;
cout << getMaximum(N, M, mat);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static int getMaximum( int N, int M, int [][] mat)
{
int global_max = Integer.MIN_VALUE;
for ( int i = 0 ; i < N; i++)
{
for ( int j = i + 1 ; j < N; j++)
{
int row_min = Integer.MAX_VALUE;
for ( int k = 0 ; k < M; k++)
{
int m = Math.max(mat[i][k],
mat[j][k]);
row_min = Math.min(row_min, m);
}
global_max = Math.max(global_max,
row_min);
}
}
return global_max;
}
public static void main(String[] args)
{
int [][] mat = { { 5 , 0 , 3 , 1 , 2 },
{ 1 , 8 , 9 , 1 , 3 },
{ 1 , 2 , 3 , 4 , 5 },
{ 9 , 1 , 0 , 3 , 7 },
{ 2 , 3 , 0 , 6 , 3 },
{ 6 , 4 , 1 , 7 , 0 } };
int N = 6 , M = 5 ;
System.out.println(getMaximum(N, M, mat));
}
}
|
Python3
import sys
def getMaximum(N, M, mat):
global_max = - 1 * (sys.maxsize)
for i in range ( 0 , N):
for j in range (i + 1 , N):
row_min = sys.maxsize
for k in range ( 0 , M):
m = max (mat[i][k], mat[j][k])
row_min = min (row_min, m)
global_max = max (global_max,
row_min)
return global_max
if __name__ = = '__main__' :
mat = [ [ 5 , 0 , 3 , 1 , 2 ],
[ 1 , 8 , 9 , 1 , 3 ],
[ 1 , 2 , 3 , 4 , 5 ],
[ 9 , 1 , 0 , 3 , 7 ],
[ 2 , 3 , 0 , 6 , 3 ],
[ 6 , 4 , 1 , 7 , 0 ] ]
N = 6
M = 5
print (getMaximum(N, M, mat))
|
C#
using System;
class GFG{
static int getMaximum( int N, int M, int [,] mat)
{
int global_max = int .MinValue;
for ( int i = 0; i < N; i++)
{
for ( int j = i + 1; j < N; j++)
{
int row_min = int .MaxValue;
for ( int k = 0; k < M; k++)
{
int m = Math.Max(mat[i, k],
mat[j, k]);
row_min = Math.Min(row_min, m);
}
global_max = Math.Max(global_max,
row_min);
}
}
return global_max;
}
public static void Main()
{
int [,] mat = { { 5, 0, 3, 1, 2 },
{ 1, 8, 9, 1, 3 },
{ 1, 2, 3, 4, 5 },
{ 9, 1, 0, 3, 7 },
{ 2, 3, 0, 6, 3 },
{ 6, 4, 1, 7, 0 } };
int N = 6, M = 5;
Console.WriteLine(getMaximum(N, M, mat));
}
}
|
Javascript
<script>
function getMaximum(N, M, mat)
{
let global_max = Number.MIN_VALUE;
for (let i = 0; i < N; i++)
{
for (let j = i + 1; j < N; j++)
{
let row_min = Number.MAX_VALUE;
for (let k = 0; k < M; k++)
{
let m = Math.max(mat[i][k],
mat[j][k]);
row_min = Math.min(row_min, m);
}
global_max = Math.max(global_max,
row_min);
}
}
return global_max;
}
let mat = [ [ 5, 0, 3, 1, 2 ],
[ 1, 8, 9, 1, 3 ],
[ 1, 2, 3, 4, 5 ],
[ 9, 1, 0, 3, 7 ],
[ 2, 3, 0, 6, 3 ],
[ 6, 4, 1, 7, 0 ] ];
let N = 6, M = 5;
document.write(getMaximum(N, M, mat));
</script>
|
Time Complexity: O(M*N2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...