Saddle point in a matrix
Last Updated :
02 Aug, 2022
Given a matrix of n x n size, the task is to find the 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 the maximum in its column.
Examples :
Input: Mat[3][3] = { {1, 2, 3},
{4, 5, 6},
{7, 8, 9}}
Output: 7
7 is minimum in its row and maximum in its column.
Input: Mat[3][3] = {{1, 2, 3},
{4, 5, 6},
{10, 18, 4}}
Output: No saddle point
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 the below steps.
Traverse all rows one by one and do the following for every row i.
- Find the minimum element of the current row and store the column index of the minimum element.
- Check if the row minimum element is also maximum in its column. We use the stored column index here.
- If yes, then saddle point else continues till the end of the matrix.
Below is the implementation of the above steps.
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100;
bool findSaddlePoint( int mat[MAX][MAX], int n)
{
for ( int i = 0; i < n; i++)
{
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;
}
}
int k;
for (k = 0; k < n; k++)
if (min_row < mat[k][col_ind])
break ;
if (k == n)
{
cout << "Value of Saddle Point " << min_row;
return true ;
}
}
return false ;
}
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;
}
|
C
#include <stdio.h>
#include <stdbool.h>
#define MAX 100
bool findSaddlePoint( int mat[MAX][MAX], int n)
{
for ( int i = 0; i < n; i++)
{
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;
}
}
int k;
for (k = 0; k < n; k++)
if (min_row < mat[k][col_ind])
break ;
if (k == n)
{
printf ( "Value of Saddle Point %d" ,min_row);
return true ;
}
}
return false ;
}
int main()
{
int mat[MAX][MAX] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
int n = 3;
if (findSaddlePoint(mat, n) == false )
printf ( "No Saddle Point " );
return 0;
}
|
Java
class Test
{
static boolean findSaddlePoint( int mat[][ ], int n)
{
for ( int i = 0 ; i < n; i++)
{
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;
}
}
int k;
for (k = 0 ; k < n; k++)
if (min_row < mat[k][col_ind])
break ;
if (k == n)
{
System.out.println( "Value of Saddle Point " + min_row);
return true ;
}
}
return false ;
}
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 " );
}
}
|
Python3
def findSaddlePoint(mat, n):
for i in range (n):
min_row = mat[i][ 0 ];
col_ind = 0 ;
for j in range ( 1 , n):
if (min_row > mat[i][j]):
min_row = mat[i][j];
col_ind = j;
k = 0 ;
for k in range (n):
if (min_row < mat[k][col_ind]):
break ;
k + = 1 ;
if (k = = n):
print ( "Value of Saddle Point " ,
min_row);
return True ;
return False ;
if __name__ = = '__main__' :
mat = [[ 1 , 2 , 3 ],
[ 4 , 5 , 6 ],
[ 7 , 8 , 9 ]];
n = 3 ;
if (findSaddlePoint(mat, n) = =
False ):
print ( "No Saddle Po" );
|
C#
using System;
class GFG {
static bool findSaddlePoint( int [,] mat,
int n)
{
for ( int i = 0; i < n; i++)
{
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;
}
}
int k;
for (k = 0; k < n; k++)
if (min_row < mat[k, col_ind])
break ;
if (k == n)
{
Console.WriteLine( "Value of Saddle Point "
+ min_row);
return true ;
}
}
return false ;
}
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 " );
}
}
|
PHP
<?php
$MAX = 100;
function findSaddlePoint( $mat , $n )
{
for ( $i = 0; $i < $n ; $i ++)
{
$min_row = $mat [ $i ][0];
$col_ind = 0;
for ( $j = 1; $j < $n ; $j ++)
{
if ( $min_row > $mat [ $i ][ $j ])
{
$min_row = $mat [ $i ][ $j ];
$col_ind = $j ;
}
}
$k ;
for ( $k = 0; $k < $n ; $k ++)
if ( $min_row < $mat [ $k ][ $col_ind ])
break ;
if ( $k == $n )
{
echo "Value of Saddle Point " ,
$min_row ;
return true;
}
}
return false;
}
$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 " ;
?>
|
Javascript
<script>
function findSaddlePoint(mat, n)
{
for (let i = 0; i < n; i++)
{
let min_row = mat[i][0], col_ind = 0;
for (let j = 1; j < n; j++)
{
if (min_row > mat[i][j])
{
min_row = mat[i][j];
col_ind = j;
}
}
let k;
for (k = 0; k < n; k++)
if (min_row < mat[k][col_ind])
break ;
if (k == n)
{
document.write( "Value of Saddle Point " + min_row+ "<br>" );
return true ;
}
}
return false ;
}
let mat = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]];
let n = 3;
if (findSaddlePoint(mat, n) == false )
document.write( "No Saddle Point " );
</script>
|
Output
Value of Saddle Point 7
Time Complexity: O(n*n)
Auxiliary Space: O(1)
Exercise :
Can there be more than one Saddle Points in a Matrix?
Share your thoughts in the comments
Please Login to comment...