Thursday, July 12, 2012

Algorithm Problem

Gift to his Child understand the code if you guys have any problem please let me know .

Our Chef is very happy that his son was selected for training in one of the finest culinary schools of the world. So he and his wife decide to buy a gift for the kid as a token of appreciation. Unfortunately, the Chef hasn't been doing good business lately, and is in no mood on splurging money. On the other hand, the boy's mother wants to buy something big and expensive. To settle the matter like reasonable parents, they play a game.
They spend the whole day thinking of various gifts and write them down in a huge matrix. Each cell of the matrix contains the gift's cost. Then they decide that the mother will choose a row number r while the father will choose a column number c, the item from the corresponding cell will be gifted to the kid in a couple of days.
The boy observes all of this secretly. He is smart enough to understand that his parents will ultimately choose a gift whose cost is smallest in its row, but largest in its column. If no such gift exists, then our little chef has no option but to keep guessing. As the matrix is huge, he turns to you for help.
He knows that sometimes the gift is not determined uniquely even if a gift exists whose cost is smallest in its row, but largest in its column. However, since the boy is so smart, he realizes that the gift's cost is determined uniquely. Your task is to tell him the gift's cost which is smallest in its row, but largest in its column, or to tell him no such gift exists.

Input

First line contains two integers R and C, the number of rows and columns in the matrix respectively. Then follow R lines, each containing C space separated integers - the costs of different gifts.

Output

Print a single integer - a value in the matrix that is smallest in its row but highest in its column. If no such value exists, then print "GUESS" (without quotes of course)

Constraints

1 <= R, C <= 100
All gift costs are positive and less than 100000000 (10^8)

Example 1

Input:
2 3
9 8 8
2 6 11

Output:
8

Example 2

Input:
3 3
9 8 11
2 6 34
5 9 11

Output:
GUESS

Example 3

Input:
2 2
10 10
10 10

Output:
10

1 comment:

Unknown said...

Hi Guys i have coded this algo and it's complexity is n to n2 not more than you can check out

if you want in c language just mail me on arungupta143@gmail.com

import java.util.*;
public class Child
{
public static void main (String [] args)
{
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
int j = sc.nextInt();
int min[] = new int [i];
int [] max = new int [j];
int [] pos_min = new int [i];
int [] pos_max = new int [j];
int array [] []= new int[i][j];
int minn = 65000;
int maxx = 0, pos_minn = 0, pos_maxx = 0;
int count = 0;
int k = 0, l = 0;
/*for(int k = 0 ; k min [k] ) {
//System.out.println("called0");
call = 0;
break ;

}

}
if(call == 1){
System.out.println(min[k]);
guess= 1;
break;
}
}
if(guess == 0)
System.out.println("Guess");


}
}