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++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the maximum of // minimum of array constructed from // any two rows of the given matrix int getMaximum(int N, int M, vector<vector<int> > mat) { // Initialize global max as INT_MIN int global_max = INT_MIN; // Iterate through the rows for (int i = 0; i < N; i++) { // Iterate through remaining rows for (int j = i + 1; j < N; j++) { // Initialize row_min as INT_MAX int row_min = INT_MAX; // Iterate through the column // values of two rows for (int k = 0; k < M; k++) { // Find max of two elements int m = max(mat[i][k], mat[j][k]); // Update the row_min row_min = min(row_min, m); } // Update the global_max global_max = max(global_max, row_min); } } // Print the global max return global_max; } // Driver Code int main() { // Given matrix mat[][] 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 } }; // Given number of rows and columns int N = 6, M = 5; // Function Call cout << getMaximum(N, M, mat); return 0; } |
3
Time Complexity: O(M*N2)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Split array into K subsets to maximize their sum of maximums and minimums
- Maximize product of same-indexed elements of same size subsequences
- Rearrange two given arrays to maximize sum of same indexed elements
- Maximums from array when the maximum decrements after every access
- Rearrange two given arrays such that sum of same indexed elements lies within given range
- Construct array with sum of product of same indexed elements in the given array equal to zero
- Rearrange array to make Bitwise XOR of similar indexed elements of two arrays is same
- Reverse a subarray to maximize sum of even-indexed elements of given array
- Modify given array to make sum of odd and even indexed elements same
- Maximize sum of pairwise products generated from the given Arrays
- Minimum swaps of same indexed elements required to obtain a Majority Element in one of the arrays
- Find the sum of elements of the Matrix generated by the given rules
- Minimum possible Bitwise OR of all Bitwise AND of pairs generated from two given arrays
- Maximize sum by selecting M elements from the start or end of rows of a Matrix
- Maximize count of rows consisting of equal elements by flipping columns of a Matrix
- Bitwise XOR of a submatrix of a matrix generated from a given array
- Rearrange array such that sum of same indexed elements is atmost K
- Rearrange an Array such that Sum of same-indexed subsets differ from their Sum in the original Array
- Find the element in the matrix generated by given rules
- Find determinant of matrix generated by array rotation
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

