Examples :
Input: lst = [3.64, 5.2, 9.42, 9.35, 8.5, 8], K = 9.1 Output: 9.35 Input: lst = [9, 11, 5, 3, 25, 18], K = 6 Output: 5
Method # 1: Using the min()
In this approach, we use the min
method from Python and apply a key that finds the absolute difference of each element with K and returns the element with the smallest difference.
|
Exit:
9.35
Method # 2: Using numpy
This approach applies the same method, but using the NumPy module. First, we convert the given list to an array. Find the absolute difference with K of each element and return the minimum of it.
| tr>
Exit :
9.35