write a program that will search for the “saddle points” in 5×5 array of integers. A saddle point is a cell whose value is grater than or equal to any other cell in it’s column. There may be more than one saddle point in the array. The program will print the coordinates of saddle points if any. The program will print “No saddle Points” if there are no saddle points.
For Example
for the input provoided as follows :
0 1 1 1 2
8 9 11 12 3
10 11 12 13 4
22 23 24 25 5
26 27 28 29 10
The output of the above input will be :
1,5
where, 1 indicates row number and 5 indicates column number


Another example
For the input provided as follows:
19 18 16 15 12
11 10 10 10 9
9 9 9 8 8
7 6 7 6 6
6 6 6 6 7
the output of the program will be
No saddle points
Algorithm to Solve
- Take 5×5 input 2D matrix as input from user.
- with the help of 2 nested for loops iterate through elements of 2D matrix.
- Inside second for loop use 2 more for loops 1 for checking the row elements conditio and another one for checking coloumn conditions.
- In problem it is given the current element should be greater than or equal to any other element in that row, so run a for loop starting from zero and running upto end of row size. But keep in mind you should not compare the number with itself, so use another condition (j!=k) in below code .
- After thet in 4th step keep one flag initially false, if it satifyies all the conditions of 4th step then make it true.
- Then write another for loop to check the column conditions, same as that of 4th step but here keep coloumn constant just change the row also here again we should not compare coloumn element with itself so write (j!=l) in below program.
- At the end just check if both the flags are true if YES then print (i+1 , j+1) because in matrix rows and coloumn number starts from 1 but in java they start from ZERO so we are adding 1 to result.
- For negative condition we just need to print “no saddle points ” selected only once so use a counter which is initially zero, at the end check if it’s value is exactly equal to zero, if YES the print no saddle points.
- So this is it you just solved one off camus coding problem.
JAVA CODE
package arrayPrograms; import java.util.*; class Problem15 { public static void main(String[] args) { //**********Coded by VRASHIKESH PATIL ***********// int[][] array1= {{0,1,1,1,2},{8,9,11,12,3},{10,11,12,13,4},{22,23,24,25,5},{26,27,28,29,10}}; boolean flag1=false; boolean flag2=false; int counter=0; for(int i=0;i<array1.length;i++) { for(int j=0;j<array1[i].length;j++) { // to check the row conditions the below loop is used for(int k=0;k<array1[i].length;k++) { if(array1[i][j]>=array1[i][k] && j!=k) { flag1=true; } else { if(j==k) { continue; } else { flag1=false; break; } } } // to check the column conditions the below loop is used for(int l=0;l<array1.length;l++) { if(array1[i][j]<=array1[l][j] && j!=l) { flag2=true; } else { if(j==l) { continue; } else { flag2=false; break; } } } // to check both row and column conditions below segment is used if(flag1 && flag2) { int res1=i+1; int res2=j+1; System.out.println(res1+"," +res2); counter++; flag1=false; flag2=false; } } } if(counter==0) { System.out.println("No saddle points"); } } }
Watch below video for complete step by step explanation for above code
Similar off Campus coding problems
Hacker Earth Coding Question with Solution
Hacker Rank Off Campus Coding Question Solved