File tree 1 file changed +39
-0
lines changed
foundation/2d-arrays/saddle-point
1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ /* Saddle Point
2
+ Time Complexity: O(n^2) & Space Complexity: O(1)
3
+ */
4
+
5
+ class Solution {
6
+
7
+ void saddlePoint (int arr [][], int n ) {
8
+
9
+ for (int i =0 ; i <n ; i ++){
10
+
11
+ int minr = arr [i ][0 ], colno = 0 ;
12
+
13
+ // finding least value in a row
14
+ for (int j =0 ; j <n ; j ++){
15
+ if ( arr [i ][j ] < minr ){ // if current elem is less than min number
16
+ colno = j ;
17
+ minr = arr [i ][j ];
18
+ }
19
+ }
20
+
21
+ // finding max value in a specific column
22
+ boolean saddlePoint = true ;
23
+ for (int k =0 ; k <n ; k ++){
24
+ if ( arr [k ][colno ] > minr ){ // if current elem is more than min number
25
+ saddlePoint = false ;
26
+ break ;
27
+ }
28
+ }
29
+
30
+ if ( saddlePoint == true ){
31
+ System .out .print ( minr );
32
+ return ;
33
+ }
34
+
35
+ }
36
+
37
+ System .out .print ("Invalid input" );
38
+ }
39
+ }
You can’t perform that action at this time.
0 commit comments