Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Puzzle Description
3.
Solution
4.
Frequently Asked Questions
4.1.
How should we solve the puzzles during the interview?
4.2.
Best tips for preparing for puzzles for interviews?
4.3.
Should we always reach the correct answer to the puzzle during the interview?
5.
Conclusion
Last Updated: Mar 27, 2024

Monkey and the Coconut Problem

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM
Interview Puzzles

Introduction

Puzzles are good exercise for the brain. They help in enhancing the cognitive abilities of the brain helping with Problem Solving and related skills. There are numerous types of puzzles; each one having a logic inherent to itself which helps in cracking it. A good puzzle well is actually like a good mystery that we may have read about or watched on TV. It has the finest of hints which help in reaching its solution.

The following article discusses one such puzzle so let's get right to it.

Puzzle Description

Monkey and the coconut is a mathematical puzzle that is said to be created by the Nobel Physicist (Quantum Physics) Prof Paul Dirac. The problem is a Diophantine problem, which means that the number of available equations is less than the number of variables.

The puzzle is as follows: 

Five men and a monkey were shipwrecked on an island. They spent the first-day gathering coconuts. During the night, one man woke up and decided to take his share of the coconuts. He divided them into five piles. One coconut was leftover so he gave it to the monkey, then hid his share and went back to sleep.

Soon a second man woke up and did the same thing. After dividing the coconuts into five piles, one coconut was leftover which he gave to the monkey. He then hid his share and went back to bed. The third, fourth, and fifth man followed exactly the same procedure. The next morning, after they all woke up, they divided the remaining coconuts into five equal shares. This time also 1 coconut was left. What is the smallest number of coconuts that could have been there in the original pile?

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Solution

Let the original number of coconuts be N. The first man divided the coconuts into five equal shares leaving one coconut. This can be represented as:

N = 5n1+1

 

After dividing the coconuts, the first man hid his share and gave one coconut to the monkey. Therefore, the remaining coconuts are 4n1.

After that, the second man divided the remaining coconuts into five equal shares leaving one coconut. This can be represented as:

4n1= 5n2+1

 

The remaining coconuts are 4n2.

Now the third man does the same. The remaining equations are:

4n2= 5n3+1, divided by the third man

4n3= 5n4+1, divided by the fourth man

4n4= 5n5+1, divided by the fifth man

4n5= 5n6+1, This is the final division. Each man gets a share of ncoconuts leaving 1 coconut for the monkey.

 

Now, working backward from the last equation, 4n5= 5n6+1, adding 4 to this equation:

4n5+4= 5n6+5

4(n5+1)= 5(n6+1), this is an important relation between nand n6. We get that (n5 + 1) must have a factor of 5.

 

Now, for the next equation:

4(n4+1)= 5(n5+1)

 

We know that (n5 + 1) has a factor of 5. Thus, it can be written as 5k, where k is a constant. The equation can be written as 4(n4+1)= 5*5k. From this, we get to know that (n4+1) has a factor of 25.

Working all the way along with the first equation, we get:

(n3 + 1) has a factor of 5*5*5.

(n2 + 1) has a factor of 5*5*5*5.

(n1 + 1) has a factor of 5*5*5*5*5.

 

Now, add 4 to the first equation:

N+4 = 5n1+5

N+4 = 5(n1+1)

n1 has a factor of 5*5*5*5*5 or 3125. The equation can be written as N+4 = 5*3125

 

Note: (n1+1) may have factors other than 3125, but for minimizing, we are taking the least possible factors.

N+4 =15625 

N=15621

Therefore, the smallest possible value of the initial number of coconuts is 15621.

Answer: 15621

Check out this problem - 8 Queens Problem

Frequently Asked Questions

How should we solve the puzzles during the interview?

Think about the problem and try to understand the question. Ask for clarifications if you feel that you do not understand the puzzle. Getting the right answer is not as important as your approach. Always explain your approach while answering.

Best tips for preparing for puzzles for interviews?

The best approach is practicing different puzzles and solving the previously asked puzzles.

Should we always reach the correct answer to the puzzle during the interview?

You should focus on your approach and your ability to convey it to the interviewer. Sometimes, you may not be able to reach the correct answer, but if your approach is right, it does not matter much.

Conclusion

In this blog, we solved a very famous puzzle known as the Monkey and the coconut problem. We looked at its solution extensively and saw the mathematical approach to solve this problem.

Recommended Readings:


Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Live masterclass