Wednesday, April 9, 2014

Given an NxN matrix with unique integers : Find and print positions of all numbers such that it is the biggest in its row and also the smallest in its collumn.

Given an NxN matrix with unique integers : Find and print positions of all numbers such that it is the biggest in its row and also the smallest in its collumn.

eg : In 3 x 3 with elements 

1 2 3  
4 5 6 
7 8 9 

the number is 3 and position (0,2)

solution --->

    find the max value at for i th  row and store its j th index . and then find if it the minimum in that column the this is the number we want .

#include<stdio.h>
#define n 3

void Max_in_row_min_in_col(int arr[n][n]){

int i,j,k;

       for(i=0;i<n;i++){
           int min,max=arr[i][0];
           int index=0 ;
        for(j=0;j<n;j++){
            if(arr[i][j] >max ){
                max=arr[i][j];
                index=j;
            }
        }

        min=max;

        for(k=0;k<n;k++)
            if(arr[k][index]<min)
                  min=arr[k][index];

        if(min==max)
            printf("the number is %d  and present at ( %d , %d )",min , i , index);
       }
}

void main(){
int arr[][n]={{1,2,3},
              {4,5,6},
              {7,8,9}};

  Max_in_row_min_in_col(arr);
}

No comments:

Post a Comment