Friday 25 October 2013

Saddle Point

import java.io.*;
public class SaddlePoint
{
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 static int row,lrow,prow,col,hcol,pcol,ARR[][],flag=0;

 void input()throws Exception
 {
     System.out.print("Enter the size of ROW : ");
     row=Integer.parseInt(br.readLine());
     System.out.print("Enter the size of COLOUMN : ");
     col=Integer.parseInt(br.readLine());
     ARR=new int[row][col];
     for(int i=0;i<row;i++)
         for(int j=0;j<col;j++
         {
             System.out.print("Enter the element of ROW "+(i+1)+" and COLOUMN "+(j+1)+" : ");
             ARR[i][j]=Integer.parseInt(br.readLine());
         }
 }

 void findSaddle()
 {
     for(int i=0;i<row;i++)
     {
         lrow=ARR[i][0];prow=0;
         for(int j=0;j<col;j++)
             if(ARR[i][j]<lrow)
               {
                   lrow=ARR[i][j];
                   prow=j;
               }
         hcol=ARR[0][prow];pcol=0;
         for(int k=0;k<row;k++)
            if(ARR[k][prow]>hcol)
            {
               hcol=ARR[k][prow];
               pcol=k;
            }
         if(lrow==hcol)
         {
           System.out.println("\nSaddle Point found at Position Row "+(prow+1)+" Coloumn "+(pcol+1)+" . Element Present = "+hcol);
           flag=1;
         }
     }
 }

 public static void main(String args[])throws Exception
 {
     SaddlePoint obj=new SaddlePoint();
     obj.input();
     System.out.println("\nOriginal Matrix :");
     for(int i=0;i<row;i++)
     {  for(int j=0;j<col;j++)
           System.out.print(ARR[i][j]+" ");
        System.out.println();
     }
     obj.findSaddle();
     if(flag==0)
       System.out.println("\nNo Saddle Point Found in Matrix");
 }
}

No comments:

Post a Comment