Search in a row wise and column wise sorted matrix | Java program to search an element in row - wise and column - wise sorted matrix ; Searches the element x in mat [ ] [ ] . If the element is found , then prints its position and returns true , otherwise prints " not ▁ found " and returns false ; traverse through the matrix ; if the element is found ; Driver code
class GFG { static int search ( int [ ] [ ] mat , int n , int x ) { if ( n == 0 ) return - 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) if ( mat [ i ] [ j ] == x ) { System . out . print ( " Element ▁ found ▁ at ▁ ( " + i + " , ▁ " + j + ")NEW_LINE"); return 1 ; } } System . out . print ( " ▁ Element ▁ not ▁ found " ) ; return 0 ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 10 , 20 , 30 , 40 } , { 15 , 25 , 35 , 45 } , { 27 , 29 , 37 , 48 } , { 32 , 33 , 39 , 50 } } ; search ( mat , 4 , 29 ) ; } }