/**
 * Given a sorted list of floating-point numbers 'arr' in ascending order and a target number 'num',
 * the function should return the index of the closest number to 'num' in the list 'arr'.
 * If there are multiple numbers equidistant from 'num', return the index of the smallest index among them.
 *
 * Example:
 * arr = [1.11, 2.22, 3.33, 4.44, 5.55]
 * num = 3.56
 *
 * The closest number to 3.56 is 3.33 at index 2.
 * Hence, the function should return 2.
 */
fun findClosest(arr: List<Double>, num: Double): Int {

    var left = 0
    var right = arr.size - 1

    while (left <= right) {
        val mid = (left + right) / 2

        if (arr[mid] == num) {
            return mid
        }

        if (Math.abs(arr[mid] - num) < 1e-12) {
            return mid
        }

        if (arr[mid] < num) {
            left = mid + 1
        } else {
            right = mid - 1
        }
    }

    return if (left == arr.size) arr.size - 1 else left
}
/**
 * Given a sorted list of floating-point numbers 'arr' in ascending order and a target number 'num',
 * the function should return the index of the closest number to 'num' in the list 'arr'.
 * If there are multiple numbers equidistant from 'num', return the index of the smallest index among them.
 *
 * Example:
 * arr = [1.11, 2.22, 3.33, 4.44, 5.55]
 * num = 3.56
 *
 * The closest number to 3.56 is 3.33 at index 2.
 * Hence, the function should return 2.
 */
fun findClosest(arr: List<Double>, num: Double): Int {
/**
 * Given a sorted list of floating-point numbers 'arr' in ascending order and a target number 'num',
 * the function should return the index of the closest number to 'num' in the list 'arr'.
 * If there are multiple numbers equidistant from 'num', return the index of the smallest index among them.
 *
 * Example:
 * arr = [1.11, 2.22, 3.33, 4.44, 5.55]
 * num = 3.56
 *
 * The closest number to 3.56 is 3.33 at index 2.
 * Hence, the function should return 2.
 */
fun findClosest(arr: List<Double>, num: Double): Int {

    var left = 0
    var right = arr.size - 1

    while (left <= right) {
        val mid = (left + right) / 2

        if (arr[mid] == num) {
            return mid
        }

        if (Math.abs(arr[mid] - num) < 1e-12) {
            return mid
        }

        if (arr[mid] < num) {
            left = mid + 1
        } else {
            right = mid - 1
        }
    }

    return if (left == arr.size) arr.size - 1 else left
}