about | help | code help+videos | done | prefs |
Write a method that returns the index of the integer that is closest to the 'target' value. How ties are handled depends on the value of 'first'. If 'first' is true, then return the index of the first (lowest index) occurrence of a tie for closest. However, if 'first' is false, return the index of the last (highest index) occurrence of a tie. For example, calling this method with nums={5,23,72,18,21} and target=22 and first=false would return a 4 because 23 and 21 tie for closest to the target, but since first is false we want the one that occurs last. Note that there might not be a tie for closest. If there is only one closest number in the array then the same index will be returned no matter what the value of 'first' is. As a precondition, you may assume that 'nums' contains at least one value. test2020_10_15_SLHL_closest([50, 55, 44, 47, 18, 22, 51, 89, 91, 100, 104, 12, 13, 74], 45, true) → 2 test2020_10_15_SLHL_closest([84], 19, true) → 0 test2020_10_15_SLHL_closest([924], 1, false) → 0 ...Save, Compile, Run (ctrl-enter) |
Progress graphs:
Your progress graph for this problem
Random user progress graph for this problem
Random Epic Progress Graph
Difficulty: 260
Copyright Nick Parlante 2017 - privacy