Retired

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
Algorithms
Arrays
Logic
Mathematics

More By Author:

Check out these other kata created by Endre-Kodehode

Stats:

CreatedJan 23, 2023
Warriors Trained15
Total Skips0
Total Code Submissions56
Total Times Completed12
JavaScript Completions12
Total Stars1
% of votes with a positive feedback rating72% of 9
Total "Very Satisfied" Votes5
Total "Somewhat Satisfied" Votes3
Total "Not Satisfied" Votes1
Total Rank Assessments7
Average Assessed Rank
7 kyu
Highest Assessed Rank
6 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • Endre-Kodehode Avatar
Ad