/**
 * Given a list of integers [data] and a limit, find the smallest span (difference between the highest
 * and lowest values) that is greater than or equal to the [limit]. Return the smallest span found.
 *
 * @param data A list of integers.
 * @param limit The minimum span limit.
 * @return The smallest span found.
 *
 * Example:
 * findSmallestSpan(listOf(10, 6, 8, 3, 9, 2, 12, 7), 5) returns 5
 * findSmallestSpan(listOf(1, 2, 3, 4, 5), 10) returns -1
 */
fun findSmallestSpan(data: List<Int>, limit: Int): Int {

    var smallestSpan = -1  // Default value if no span is found greater than or equal to the limit
    for (i in data.indices) {
        for (j in i until data.size) {
            val span = data.subList(i, j + 1).maxOrNull()!! - data.subList(i, j + 1).minOrNull()!!
            if (span >= limit) {
                if (smallestSpan == -1 || span < smallestSpan) {
                    smallestSpan = span
                }
            }
        }
    }
    return smallestSpan
}
/**
 * Given a list of integers [data] and a limit, find the smallest span (difference between the highest
 * and lowest values) that is greater than or equal to the [limit]. Return the smallest span found.
 *
 * @param data A list of integers.
 * @param limit The minimum span limit.
 * @return The smallest span found.
 *
 * Example:
 * findSmallestSpan(listOf(10, 6, 8, 3, 9, 2, 12, 7), 5) returns 5
 * findSmallestSpan(listOf(1, 2, 3, 4, 5), 10) returns -1
 */
fun findSmallestSpan(data: List<Int>, limit: Int): Int {
/**
 * Given a list of integers [data] and a limit, find the smallest span (difference between the highest
 * and lowest values) that is greater than or equal to the [limit]. Return the smallest span found.
 *
 * @param data A list of integers.
 * @param limit The minimum span limit.
 * @return The smallest span found.
 *
 * Example:
 * findSmallestSpan(listOf(10, 6, 8, 3, 9, 2, 12, 7), 5) returns 5
 * findSmallestSpan(listOf(1, 2, 3, 4, 5), 10) returns -1
 */
fun findSmallestSpan(data: List<Int>, limit: Int): Int {

    var smallestSpan = -1  // Default value if no span is found greater than or equal to the limit
    for (i in data.indices) {
        for (j in i until data.size) {
            val span = data.subList(i, j + 1).maxOrNull()!! - data.subList(i, j + 1).minOrNull()!!
            if (span >= limit) {
                if (smallestSpan == -1 || span < smallestSpan) {
                    smallestSpan = span
                }
            }
        }
    }
    return smallestSpan
}