/**
 * Given two lists of ranges, find the intersection between them.
 *
 * @param a Two lists of ranges where each range is represented by a Pair of two integers.
 * @param b Two lists of ranges where each range is represented by a Pair of two integers.
 * @return A list of common ranges between a and b, represented by Pairs of two integers.
 *
 * Example:
 * findCommonRanges(listOf(Pair(1, 5), Pair(7, 10), Pair(15, 20)), listOf(Pair(3, 6), Pair(8, 12), Pair(17, 19)))
 * Output: [(3, 5), (8, 10), (17, 19)]
 */
fun findCommonRanges(a: List<Pair<Int, Int>>, b: List<Pair<Int, Int>>): List<Pair<Int, Int>> {

    val results = mutableListOf<Pair<Int, Int>>()

    for (rangeA in a) {
        for (rangeB in b) {
            val (startA, endA) = rangeA
            val (startB, endB) = rangeB

            if (startA <= endB && startB <= endA) {
                val start = maxOf(startA, startB)
                val end = minOf(endA, endB)
                results.add(Pair(start, end))
            }
        }
    }

    return results
}
/**
 * Given two lists of ranges, find the intersection between them.
 *
 * @param a Two lists of ranges where each range is represented by a Pair of two integers.
 * @param b Two lists of ranges where each range is represented by a Pair of two integers.
 * @return A list of common ranges between a and b, represented by Pairs of two integers.
 *
 * Example:
 * findCommonRanges(listOf(Pair(1, 5), Pair(7, 10), Pair(15, 20)), listOf(Pair(3, 6), Pair(8, 12), Pair(17, 19)))
 * Output: [(3, 5), (8, 10), (17, 19)]
 */
fun findCommonRanges(a: List<Pair<Int, Int>>, b: List<Pair<Int, Int>>): List<Pair<Int, Int>> {
/**
 * Given two lists of ranges, find the intersection between them.
 *
 * @param a Two lists of ranges where each range is represented by a Pair of two integers.
 * @param b Two lists of ranges where each range is represented by a Pair of two integers.
 * @return A list of common ranges between a and b, represented by Pairs of two integers.
 *
 * Example:
 * findCommonRanges(listOf(Pair(1, 5), Pair(7, 10), Pair(15, 20)), listOf(Pair(3, 6), Pair(8, 12), Pair(17, 19)))
 * Output: [(3, 5), (8, 10), (17, 19)]
 */
fun findCommonRanges(a: List<Pair<Int, Int>>, b: List<Pair<Int, Int>>): List<Pair<Int, Int>> {

    val results = mutableListOf<Pair<Int, Int>>()

    for (rangeA in a) {
        for (rangeB in b) {
            val (startA, endA) = rangeA
            val (startB, endB) = rangeB

            if (startA <= endB && startB <= endA) {
                val start = maxOf(startA, startB)
                val end = minOf(endA, endB)
                results.add(Pair(start, end))
            }
        }
    }

    return results
}