Find all matrix elements which are minimum in their row and maximum in their column | Python3 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
import sys NEW_LINE def minmaxNumbers ( matrix , res ) : NEW_LINE INDENT s = set ( ) NEW_LINE for i in range ( 0 , len ( matrix ) , 1 ) : NEW_LINE INDENT minr = sys . maxsize NEW_LINE for j in range ( 0 , len ( matrix [ i ] ) , 1 ) : NEW_LINE INDENT minr = min ( minr , matrix [ i ] [ j ] ) NEW_LINE DEDENT s . add ( minr ) NEW_LINE DEDENT for j in range ( 0 , len ( matrix [ 0 ] ) , 1 ) : NEW_LINE INDENT maxc = - sys . maxsize - 1 NEW_LINE for i in range ( 0 , len ( matrix ) , 1 ) : NEW_LINE INDENT maxc = max ( maxc , matrix [ i ] [ j ] ) NEW_LINE DEDENT if ( maxc in s ) : NEW_LINE INDENT res . append ( maxc ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mat = [ [ 1 , 10 , 4 ] , [ 9 , 3 , 8 ] , [ 15 , 16 , 17 ] ] NEW_LINE ans = [ ] NEW_LINE minmaxNumbers ( mat , ans ) NEW_LINE if ( len ( ans ) == 0 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT for i in range ( len ( ans ) ) : NEW_LINE INDENT print ( ans [ i ] ) NEW_LINE DEDENT DEDENT