# Restaurant

See the original problem on HackerRank.

## Solutions

We have to cut a rectangular bread having size $$l * b$$ into squares of equal dimension such that no piece of original bread is left over.

So we have to make cuts only vertically or horizontally. Hence we can conclude that if the length of a side of the square is $$a$$, both $$l$$ and $$b$$ has to be divisible by $$a$$.

In addition, $$a$$ has to be as large as possible.

Hence, the problem reduces to finding an integer whose value is maximum as well as divisible by both $$l$$ and $$b$$. That is, finding the greatest common divisor (gcd) of $$l$$ and $$b$$:

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17  int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int main() { int T; cin >> T; while (T--) { int l, b; cin >> l >> b; const auto _gcd = gcd(l, b); const auto ll = l/_gcd; const auto bb = b/_gcd; cout << (ll * bb) << endl; } } 

This problem is easy but it's useful to approach the problem from a purely mathematical point of view. Sometimes we just forget some basic concepts like gdc.

Another related “soft-skills challenge” is how to explain the algorithm/approach without referring to the gcd and other mathematical concepts like prime factorization and modulo operations. Imagine you can only use the 4 fundamental operations.

Another interesting point is about implementing gdc or learning if our language already provides one implementation. For instance, since C++17 we have a standard gcd function in the header numeric.

We've worked on this challenge in these gyms: modena
{"ft_lang_label":"__label__en","ft_lang_prob":0.9225217,"math_prob":0.99554515,"size":1218,"snap":"2019-51-2020-05","text_gpt3_token_len":265,"char_repetition_ratio":0.08896211,"word_repetition_ratio":0.0,"special_character_ratio":0.22906403,"punctuation_ratio":0.08035714,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9987284,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-12-09T03:27:05Z\",\"WARC-Record-ID\":\"<urn:uuid:38210298-caf4-4c2e-a9ae-489cf1f53b75>\",\"Content-Length\":\"13894\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:8d252ea4-5f36-4eef-9a83-26af5ca3c4b0>\",\"WARC-Concurrent-To\":\"<urn:uuid:eb58fcfd-0245-4c7e-abc5-48f350d50e2d>\",\"WARC-IP-Address\":\"35.185.44.232\",\"WARC-Target-URI\":\"https://coding-gym.org/challenges/restaurant/\",\"WARC-Payload-Digest\":\"sha1:FYQOCQU7ROQWZUREEVD3VHBUELWKH5D7\",\"WARC-Block-Digest\":\"sha1:YV3HDEWUEQT2Y3MIUE3UTBC27AQHNNWB\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-51/CC-MAIN-2019-51_segments_1575540517156.63_warc_CC-MAIN-20191209013904-20191209041904-00244.warc.gz\"}"}