I am trying to solve the problem
candy.My approach is that i am giving the first child 1 candy , the second child k+1 candies , the third child k+1+k candies and so on.Then by using the for formula for finding the sum of an ap series, i am calculating the minimum number of candies that is required.can anybody please point out what is wrong with my approach.
Your Approach is wrong because if you read the problem it is written that
“difference of at least K candies between two adjacent kids.”
Lets take an example-
if n=5 and k=1
1st child will get 1 candy.
2nd child will get k+1 candies.
3rd child will get 1 candy: (At this point you are wrong!)
(we will not give k+1+k candies because k+1(2nd child) and 1(3rd child) have a difference of k candies so condition is satisfied)
4th child will get k+1 candies because the third child got 1 candy and we have to make a difference of k candies.
5th child will get k candies more than the 4th child because the 5th child is adjacent to first child(Children are sitting in circle)
so 5th child (nth child) has to make a difference atleast k candies with respect to 1st child and 4th child.
So if n is even then the formula will be
n+(n/2)*k
and if n is odd then formula will be:
(n-1) +( (n-1)/2)* (k) + (2*k)+1
Just draw the figure sitting 5 students in a circle and visualise what i explained!
Thank you very much…
Welcome!