Cake-Walk

None

### PROBLEM:

Given an R x C grid of numbers, find out a number which is the smallest number in its row and the largest number in its column or report that no such number exists.

### QUICK EXPLANATION:

Do an exhaustive search - for each cell in the grid, check if it satisfies the given conditions or not.

### DETAILED EXPLANATION:

Approach 1:
The naivest approach would be to iterate over all the cells and for each cell check
if it is the smallest cell in its row and the largest cell in its column.
This approach takes time O( RC (R+C))

Approach 2:
Though previous approach is sufficient to get accepted, one can be a little smarter.
Let’s consider that at some point of time your program examines (a, b). At that time
we find out the smallest element of the row a and the largest element of column
b. When we examine (a, b+1), we’d again find out the smallest element of row a
and thus repeat the computation. It suggests that we find out the smallest
element of each row and the largest element of each column only once and store
it. This precomputation takes O(RC) time. Once it has been done, we could judge
if a given cell is desired answer or not in constant time looking at pre computed
values. Hence even this step is O(RC).

So total this approach takes O(RC) time.

### SETTER’S SOLUTION:

Can be found here.

### TESTER’S SOLUTION:

Can be found here.

3 Likes

So we have two arrays smallestrow[] and largestcolumn[], so how about checking if there exist a common element in both the arrays? if it exist then print it, otherwise print “guess”. Is this logic correct??

Yeah that’s exactly what i did: http://www.codechef.com/viewsolution/6558964
{"ft_lang_label":"__label__en","ft_lang_prob":0.8620219,"math_prob":0.9098347,"size":1395,"snap":"2023-14-2023-23","text_gpt3_token_len":343,"char_repetition_ratio":0.12508987,"word_repetition_ratio":0.064,"special_character_ratio":0.22150537,"punctuation_ratio":0.105442174,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98657674,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-05-31T18:28:00Z\",\"WARC-Record-ID\":\"<urn:uuid:1361e8ec-e1e5-4b74-9c20-ea4596ce8510>\",\"Content-Length\":\"24653\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b82fdba0-b9af-4cde-9a28-67567e74c75e>\",\"WARC-Concurrent-To\":\"<urn:uuid:34343712-fe55-45e9-9d6f-4f94fda4b565>\",\"WARC-IP-Address\":\"34.198.237.79\",\"WARC-Target-URI\":\"https://discusstest.codechef.com/t/sad-editorial/396\",\"WARC-Payload-Digest\":\"sha1:S4K42LLK2AP5HQ4ZOYSUTZABRPXZVVHX\",\"WARC-Block-Digest\":\"sha1:EV7CKDB65KDUX453D3KNJLXSIQZ7V2D4\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-23/CC-MAIN-2023-23_segments_1685224647409.17_warc_CC-MAIN-20230531182033-20230531212033-00601.warc.gz\"}"}