## Saturday, November 12, 2016

### 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.
simple solution is to traverse all matrix elements one by one and check if the element is Saddle Point or not.
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.
`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][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)`
`        ``{`
`           ``cout << ``"Value of Saddle Point "` `<< min_row;`
`           ``return` `true``;`
`        ``}`
`    ``}`

`    ``// If Saddle Point not found`
`    ``return` `false``;`
`}`