Problem 56175. Dartboard Average II
A dartboard arranges the numbers 1 to 20 such that each value is typically flanked by quite different values - for example, 20 is flanked by 1 and 5, 16 by 8 and 7, and so on.
Assuming a player of questionable skill aiming for a particular value will hit that value or the value on either side with equal probability, what is the best and worst value to aim at? That is, what value has the highest and lowest average with the two values on either side?
Given a vector v representing the arrangement of the values 1 to 20, return the values with the maximum and minimum average with the values on either side. Note that a dartboard is circular, so the values in v are assumed to wrap.
For example, a standard dartboard is represented by
v = [20 1 18 4 13 6 10 15 2 17 3 19 7 16 8 11 14 9 12 5];
For this v, your function should return 7 and 17 (in that order). The target value 7 has the highest average (mean([19 7 16]) = 14). Similarly, 17 has the lowest average (mean([2 17 3]) = 7.3333).
If there are multiple values with the highest or lowest average, your function should return the first of them. (For example, if v = [12 10 20 8 14 1 7...], both 10 and 8 have an average of 14. Your function should return 10 in this case.)
The vector v will always be a permutation of the values 1:20.
Solution Stats
Problem Comments
-
3 Comments
Dyuman Joshi
on 24 Oct 2022
There are test cases where there are multiples values for max/min.
Please make it clear that you are looking for the value that occurs first.
Matt Tearle
on 25 Oct 2022
@Dyuman Joshi: thanks, good catch. I've added that to the description.
Rafael S.T. Vieira
on 29 Oct 2022
Precision errors are a pain.
Solution Comments
Show commentsProblem Recent Solvers101
Suggested Problems
-
2605 Solvers
-
1917 Solvers
-
1226 Solvers
-
588 Solvers
-
String Array Basics, Part 1: Convert Cell Array to String Array; No Missing Values
1667 Solvers
More from this Author22
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!