# Saddle point in a matrix

Given a matrix of n x n size, the task is to find saddle point of the matrix. A saddle point is an element of the matrix such that it is the minimum element in its row and maximum in its column.

Examples :

```Input: Mat = { {1, 2, 3},
{4, 5, 6},
{7, 8, 9}}
Output: 7
7 is minimum in its row and maximum in its column.

Input: Mat = {{1, 2, 3},
{4, 5, 6},
{10, 18, 4}}
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

A simple solution is to traverse all matrix elements one by one and check if the element is Saddle Point or not.

An efficient solution is based on below steps.
Traverse all rows one by one and do following for every row i.

1. Find the minimum element of current row and store column index of the minimum element.
2. Check if the row minimum element is also maximum in its column. We use the stored column index here.
3. If yes, then saddle point else continue till end of matrix.

Below is implementation of above steps.

## C++

 `// C++ program to illustrate Saddle point ` `#include ` `using` `namespace` `std; ` ` `  `const` `int` `MAX = 100; ` ` `  `// Function to find saddle point ` `bool` `findSaddlePoint(``int` `mat[MAX][MAX], ``int` `n) ` `{ ` `    ``// Process all rows one by one ` `    ``for` `(``int` `i = 0; i < n; i++) ` `    ``{ ` `        ``// Find the minimum element of row i. ` `        ``// Also find column index of the minimum element ` `        ``int` `min_row = mat[i], col_ind = 0; ` `        ``for` `(``int` `j = 1; j < n; j++) ` `        ``{ ` `            ``if` `(min_row > mat[i][j]) ` `            ``{ ` `                ``min_row = mat[i][j]; ` `                ``col_ind = j; ` `            ``} ` `        ``} ` ` `  `        ``// Check if the minimum element of row is also ` `        ``// the maximum element of column or not ` `        ``int` `k; ` `        ``for` `(k = 0; k < n; k++) ` ` `  `            ``// Note that col_ind is fixed ` `            ``if` `(min_row < mat[k][col_ind]) ` `                ``break``; ` ` `  `        ``// If saddle point is present in this row then ` `        ``// print it ` `        ``if` `(k == n) ` `        ``{ ` `           ``cout << ``"Value of Saddle Point "` `<< min_row; ` `           ``return` `true``; ` `        ``} ` `    ``} ` ` `  `    ``// If Saddle Point not found ` `    ``return` `false``; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `mat[MAX][MAX] = {{1, 2, 3}, ` `                        ``{4, 5, 6}, ` `                        ``{7, 8, 9}}; ` `    ``int` `n = 3; ` `    ``if` `(findSaddlePoint(mat, n) == ``false``) ` `       ``cout << ``"No Saddle Point "``; ` `    ``return` `0; ` `} `

## Java

 `// Java program to illustrate Saddle point ` ` `  `class` `Test ` `{ ` `    ``// Method to find saddle point ` `    ``static` `boolean` `findSaddlePoint(``int` `mat[][    ], ``int` `n) ` `    ``{ ` `        ``// Process all rows one by one ` `        ``for` `(``int` `i = ``0``; i < n; i++) ` `        ``{ ` `            ``// Find the minimum element of row i. ` `            ``// Also find column index of the minimum element ` `            ``int` `min_row = mat[i][``0``], col_ind = ``0``; ` `            ``for` `(``int` `j = ``1``; j < n; j++) ` `            ``{ ` `                ``if` `(min_row > mat[i][j]) ` `                ``{ ` `                    ``min_row = mat[i][j]; ` `                    ``col_ind = j; ` `                ``} ` `            ``} ` `      `  `            ``// Check if the minimum element of row is also ` `            ``// the maximum element of column or not ` `            ``int` `k; ` `            ``for` `(k = ``0``; k < n; k++) ` `      `  `                ``// Note that col_ind is fixed ` `                ``if` `(min_row < mat[k][col_ind]) ` `                    ``break``; ` `      `  `            ``// If saddle point is present in this row then ` `            ``// print it ` `            ``if` `(k == n) ` `            ``{ ` `               ``System.out.println(``"Value of Saddle Point "` `+ min_row); ` `               ``return` `true``; ` `            ``} ` `        ``} ` `      `  `        ``// If Saddle Point not found ` `        ``return` `false``; ` `    ``} ` `     `  `    ``// Driver method ` `    ``public` `static` `void` `main(String[] args)  ` `    ``{ ` `        ``int` `mat[][] = {{``1``, ``2``, ``3``}, ` `                      ``{``4``, ``5``, ``6``}, ` `                     ``{``7``, ``8``, ``9``}}; ` `         `  `        ``int` `n = ``3``; ` `        ``if` `(findSaddlePoint(mat, n) == ``false``) ` `            ``System.out.println(``"No Saddle Point "``); ` `    ``} ` `} `

## C#

 `// C# program to illustrate Saddle point ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``// Method to find saddle point ` `    ``static` `bool` `findSaddlePoint(``int` `[,] mat,  ` `                                ``int` `n) ` `    ``{ ` `         `  `        ``// Process all rows one by one ` `        ``for` `(``int` `i = 0; i < n; i++) ` `        ``{ ` `             `  `            ``// Find the minimum element of  ` `            ``// row i. Also find column index ` `            ``// of the minimum element ` `            ``int` `min_row = mat[i, 0], col_ind = 0; ` `            ``for` `(``int` `j = 1; j < n; j++) ` `            ``{ ` `                ``if` `(min_row > mat[i, j]) ` `                ``{ ` `                    ``min_row = mat[i, j]; ` `                    ``col_ind = j; ` `                ``} ` `            ``} ` `     `  `            ``// Check if the minimum element  ` `            ``// of row is also the maximum  ` `            ``// element of column or not ` `            ``int` `k; ` `            ``for` `(k = 0; k < n; k++) ` `     `  `                ``// Note that col_ind is fixed ` `                ``if` `(min_row < mat[k, col_ind]) ` `                    ``break``; ` `     `  `            ``// If saddle point is present in this row then ` `            ``// print it ` `            ``if` `(k == n) ` `            ``{ ` `                ``Console.WriteLine(``"Value of Saddle Point "`  `                                                ``+ min_row); ` `                ``return` `true``; ` `            ``} ` `        ``} ` `     `  `        ``// If Saddle Point not found ` `        ``return` `false``; ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main()  ` `    ``{ ` `        ``int` `[,] mat = {{1, 2, 3}, ` `                       ``{4, 5, 6}, ` `                       ``{7, 8, 9}}; ` `         `  `        ``int` `n = 3; ` `        ``if` `(findSaddlePoint(mat, n) == ``false``) ` `            ``Console.WriteLine(``"No Saddle Point "``); ` `    ``} ` `} ` ` `  `// This code is contributed by KRV. `

## PHP

 ` ``\$mat``[``\$i``][``\$j``]) ` `            ``{ ` `                ``\$min_row` `= ``\$mat``[``\$i``][``\$j``]; ` `                ``\$col_ind` `= ``\$j``; ` `            ``} ` `        ``} ` ` `  `        ``// Check if the minimum element of  ` `        ``// row is also the maximum element ` `        ``// of column or not ` `        ``\$k``; ` `        ``for` `(``\$k` `= 0; ``\$k` `< ``\$n``; ``\$k``++) ` ` `  `            ``// Note that col_ind is fixed ` `            ``if` `(``\$min_row` `< ``\$mat``[``\$k``][``\$col_ind``]) ` `                ``break``; ` ` `  `        ``// If saddle point is present in ` `        ``// this row then print it ` `        ``if` `(``\$k` `== ``\$n``) ` `        ``{ ` `        ``echo` `"Value of Saddle Point "` `, ` `                              ``\$min_row``; ` `        ``return` `true; ` `        ``} ` `    ``} ` ` `  `    ``// If Saddle Point not found ` `    ``return` `false; ` `} ` ` `  `// Driver code ` `\$mat` `= ``array``(``array``(1, 2, 3), ` `             ``array``(4, 5, 6), ` `             ``array` `(7, 8, 9)); ` `\$n` `= 3; ` `if` `(findSaddlePoint(``\$mat``, ``\$n``) == false) ` `echo` `"No Saddle Point "``; ` ` `  `// This code is contributed by anuj_67. ` `?> `

Output :

```Value of Saddle Point 7
```

Exercise :
Can there be more than one Saddle Points in a Matrix?

This article is contributed by Sahil Chhabra(KILLER). 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.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up

Improved By : KRV, vt_m

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.