Find all matrix elements which are minimum in their row and maximum in their column | C ++ program for the above approach ; Functionto find all the matrix elements which are minimum in its row and maximum in its column ; Initialize unordered set ; Traverse the matrix ; Update the minimum element of current row ; Insert the minimum element of the row ; Update the maximum element of current column ; Checking if it is already present in the unordered_set or not ; Driver Code ; Function call ; If no such matrix element is found
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > minmaxNumbers ( vector < vector < int > > & matrix , vector < int > & res ) { unordered_set < int > set ; for ( int i = 0 ; i < matrix . size ( ) ; i ++ ) { int minr = INT_MAX ; for ( int j = 0 ; j < matrix [ i ] . size ( ) ; j ++ ) { minr = min ( minr , matrix [ i ] [ j ] ) ; } set . insert ( minr ) ; } for ( int j = 0 ; j < matrix [ 0 ] . size ( ) ; j ++ ) { int maxc = INT_MIN ; for ( int i = 0 ; i < matrix . size ( ) ; i ++ ) { maxc = max ( maxc , matrix [ i ] [ j ] ) ; } if ( set . find ( maxc ) != set . end ( ) ) { res . push_back ( maxc ) ; } } return res ; } int main ( ) { vector < vector < int > > mat = { { 1 , 10 , 4 } , { 9 , 3 , 8 } , { 15 , 16 , 17 } } ; vector < int > ans ; minmaxNumbers ( mat , ans ) ; if ( ans . size ( ) == 0 ) cout << " - 1" << endl ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) cout << ans [ i ] << endl ; return 0 ; }