Find the best average (retired)
Description:
Find the best average
Intro
It's not going to be perfect, but we'll see how close we get...
Description
Write a function that takes in an array of numbers, and a target number. Find out which two numbers in the array get closest to the target number when averaged and return the difference between that number and the target number.
To calculate the average of two numbers:
(number1 + number2) / 2 = average
The numbers in the input array can be both positive and negative.
Example
Input array:
[250, -137, -188, -34, 335, -212, -286, 83, -404, -235, -458, 437, 237,-6, -211]
Target number:
39
In this example your function should find that 83
and -6
gives an average of 38.5
, which has a difference of 0.5
to the target number 39
. No lower difference to the target number can be achieved from any other number pairing in the input array so we return 0.5
.
Rules and assertions
- If two pairings tie for closest, it doesn't matter. Just return the difference both pairings average to.
- The input array will always have a length of 2+
- The target number can be both higher or lower than any number in the input array
Similar Kata:
Stats:
Created | Jan 23, 2023 |
Warriors Trained | 15 |
Total Skips | 0 |
Total Code Submissions | 56 |
Total Times Completed | 12 |
JavaScript Completions | 12 |
Total Stars | 1 |
% of votes with a positive feedback rating | 72% of 9 |
Total "Very Satisfied" Votes | 5 |
Total "Somewhat Satisfied" Votes | 3 |
Total "Not Satisfied" Votes | 1 |
Total Rank Assessments | 7 |
Average Assessed Rank | 7 kyu |
Highest Assessed Rank | 6 kyu |
Lowest Assessed Rank | 7 kyu |