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
using System ; using System . Collections . Generic ; class GFG { public static List < int > minmaxNumbers ( int [ , ] matrix , List < int > res ) { HashSet < int > set = new HashSet < int > ( ) ; for ( int i = 0 ; i < matrix . GetLength ( 0 ) ; i ++ ) { int minr = int . MaxValue ; for ( int j = 0 ; j < matrix . GetLength ( 1 ) ; j ++ ) { minr = Math . Min ( minr , matrix [ i , j ] ) ; } set . Add ( minr ) ; } for ( int j = 0 ; j < matrix . GetLength ( 0 ) ; j ++ ) { int maxc = int . MinValue ; for ( int i = 0 ; i < matrix . GetLength ( 1 ) ; i ++ ) { maxc = Math . Max ( maxc , matrix [ i , j ] ) ; } if ( set . Contains ( maxc ) ) { res . Add ( maxc ) ; } } return res ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 1 , 10 , 4 } , { 9 , 3 , 8 } , { 15 , 16 , 17 } } ; List < int > ans = new List < int > ( ) ; ans = minmaxNumbers ( mat , ans ) ; if ( ans . Count == 0 ) Console . WriteLine ( " - 1" ) ; for ( int i = 0 ; i < ans . Count ; i ++ ) Console . WriteLine ( ans [ i ] ) ; } }