Click here to Skip to main content
15,914,795 members
Please Sign up or sign in to vote.
1.80/5 (5 votes)
See more:
Ram and Chocolates

Ram is a school boy. He has n good friends numbered from 1 to n. Let this be denoted by an Array A. The friend with higher array index is closer to Ram. It's his birthday today and hence he wants to distribute his birthday chocolates in a way such that A[i+1]=A[i]+3. Now he had n boxes numbered from 1 to n for each of his friend and his mother had filled them with some random number of chocolates, However Ram has made up his mind to distribute the the chocolates in the way described above. He wants to know if it is possible to re-arrange the chocolates in the boxes to have a distribution pattern he likes.

What I have tried:

well the question was too tricky to answer , so if any give out the answer for the question stated above....
Posted
Updated 10-Jul-17 5:52am
Comments
Richard Deeming 6-Jul-17 8:33am    
Your homework is set to test what you know, not how good you are at begging a bunch of strangers on the internet to do your work for you.

If you don't know how to answer the question, then talk to your teacher.

Well think about it.

The number of chocolates in the boxes is set at random

Rams wants a particular distribution

If there is any case where a box does not fit the distribution then the answer is "no, he can't". If all boxes align perfectly with the distribute then "yes, he can"

Now:

How do you test that all boxes can align to f(n-1) = f(n)+3 ?

Can you come up with a couple of examples of an array of numbers that follow the distribution?

What do you notice about that array of numbers in order?

Is it possible to compare any two numbers and decide that they do or do not fit the distribution?

Look into these questions. Explore the answers. You'll see what the question is getting at.

As Richard mentioned, this forum isn't suited for these kind of questions. Your teacher will want you to learn something specific from the question. They will be able to guide you more directly to that specific thing than we ever could.
 
Share this answer
 
v2
A[1] = a
A[2] = A[1] + 3 = a + 3
A[3] = A[2] + 3 = a + 6
..
A[n] = A[n-1] + 3 = a + (n-1) * 3

The sum is
S = n * a + 3 + 6 + .. + (n-1) * 3 
S = n * a + 3 * [1 + 2 + .. + (n-1)] 
S = n * a + 3 * n * (n-1) / 2

hence
a = [S - 3 * n * (n-1) / 2] / n


Compute a, if it is integer then the distrubution is feasible.
 
Share this answer
 
Quote:
well the question was too tricky to answer

The first step is to solve by hand with a sheet of paper and a pencil. This is how you build an algorithm/procedure that solve the problem.
Failing on first trial is normal. You learn and find the algorithm by "trial and error". "Trial and Error" is an efficient way of learning.
Note that partial answer is an indication that you are close to the answer.
Once you have a method to solve the problem, you have to translate code, this is your algorithm.

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
Any failure of you will help you to learn what works and what don't, it is called 'trial and error' learning.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.

The idea of "development" is as the word suggests: "The systematic use of scientific and technical knowledge to meet specific objectives or requirements." BusinessDictionary.com[^]
That's not the same thing as "have a quick google and give up if I can't find exactly the right code".
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900