Pages

Monday 9 April 2012

Saddle Point

1) Find the minimum in each row
2) Find the maximum in each column, and put these in separate arrays.
3) Print the minimum value of the column maxima (-10), and the maximum value of the row minima(-10)

import java.util.Scanner;


public class saddlepoints {
    public static void main(String args[])
    {
        int m,n;
        int arr[][] = new int[10][10];
        int rowMinima[] = new int[10];
        int colMaxima[] = new int[10];
        Scanner scanner = new Scanner(System.in);
        System.out.println("Input the number of rows");
        m = Integer.parseInt(scanner.nextLine());
        System.out.println("Input the number of columns");
        n = Integer.parseInt(scanner.nextLine());
       
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                arr[i][j] = Integer.parseInt(scanner.nextLine());
            }
        }
       
        for(int i=0;i<m;i++){
            int min = Integer.MAX_VALUE;
            for(int j=0;j<n;j++){
                if(arr[i][j]<min)
                    min = arr[i][j];
            }
            rowMinima[i] = min;
        }
       
        for(int j=0;j<n;j++){
            int max = Integer.MIN_VALUE;
            for(int i=0;i<n;i++){
                if(arr[j][i]>max)
                    max = arr[j][i];
            }
            colMaxima[j] = max;
        }
       
        int max=Integer.MIN_VALUE;
        for(int i=0;i<m;i++)
        {   
            if(rowMinima[i]>max)
                max = rowMinima[i];
        }
       
        int min=Integer.MAX_VALUE;
        for(int i=0;i<n;i++)
        {   
            if(rowMinima[i]<min)
                min = rowMinima[i];
        }
       
        if(min == max)
            System.out.println("Saddle Point is "+min);
               
    }
}
 

No comments:

Post a Comment