Candy

Alice has 10 candies arranged in a row. Each of them is assigned a "yummy value":

3 , 7 , 5 , 4 , 8 , 9 , 6 , 2 , 8 , 3. 3, \ 7, \ 5, \ 4, \ 8, \ 9, \ 6, \ 2, \ 8, \ 3.

Alice would prefer eating all of them, but her mother wants her to distribute 9 candies to her friends, and she can only eat the one left.

The mechanism to distribute the candies is as follows:

  1. Pick 3 consecutive candies and give them to her friends.
  2. Merge the other candies without changing the order.
  3. Continue until there is only 1 candy left.

Assuming that Alice distributes the candy optimally, what is the maximum "yummy value" she can save for herself?


The answer is 6.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

5 solutions

The candy with yummy 6 is the 1 with highest yummy left because you can pick 3 behind it. If you pick 3 around a higher yummy you would have another 1 left in front or behind it so you can't hold on to the high yummy because you have to give away 3 consecutive candies

Abhishek Sinha
Nov 7, 2016

Observe that, Alice can only possibly get those candies which are located initially at the 3 k + 1 3k+1 st position for some integer k 0 k\geq 0 . Because for her to get a candy located initially at position i i , she has to give all candies that are located either left or the right of that candy to her friends. The number of left and right candies, that are distributed among her friends, must be a multiple of 3 3 , because candies are picked in a group of 3 3 . Hence, Alice can only get candies with yummy values { 3 , 4 , 6 } \{3,4,6\} . Thus the maximum yummy value she can save for herself is 6 6 .

Eric Lee
Jan 26, 2021

while len(sl) >= 4: sl = sl[3:-3] if len(sl) == 4 and len(sl)%3 == 1: print(max(sl[0],sl[-1]))

Osman Ates
Feb 2, 2017
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//C# Program
// Answer 6

using System;
namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
int[] myCookies = {3,7,5,4,8,9,6,2,8,3};
int yummiest = getYummy(myCookies);
            Console.WriteLine(yummiest);
        }
        public static int getYummy(int[] cookieArray)
{
int yummy = cookieArray[0];
    for(int i=0;i<cookieArray.Length-1;i+=3)
{
if(cookieArray[i]>yummy)yummy = cookieArray[i];
}
return yummy;
}

    }
}

Saswata Naha
Oct 13, 2016

She had to get high yummy value possible.

  1. If she start from anyone end she will get 3,she doesn't want so less.

  2. Now she can choose 3 from each end, then there will remain 4 8 9 6. Now if she choose 8 9 6 ,she will get 4. but, if she take 4 8 9 , she will get 6. 6 is higher! DONE!!

She can take any sequence of 3 consecutive candies after merging, so you need to consider more cases.

For example, she could have taken "5, 4, 8 " and then "7, 9, 6" (because the 5, 4, 8 is gone).

Calvin Lin Staff - 4 years, 7 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...