/**
 * Given two segments represented as pairs of integers, segment1 and segment2,
 * returns the intersection of the segments if it exists, otherwise returns null.
 *
 * @param segment1 A pair representing the first segment as (start1, end1)
 * @param segment2 A pair representing the second segment as (start2, end2)
 * @return A pair representing the intersection of the segments as (start, end), or null if no intersection.
 *
 * Example:
 * intersectionOfSegments(Pair(1, 5), Pair(4, 7)) // Output: Pair(4, 5)
 * intersectionOfSegments(Pair(-100, 0), Pair(100, 200)) // Output: null
 */
fun intersectionOfSegments(segment1: Pair<Int, Int>, segment2: Pair<Int, Int>): Pair<Int, Int>? {

    // Check if there is any overlap between the segments
    if (segment1.first > segment2.second || segment2.first > segment1.second) {
        return null
    }

    // Determine the start and end values of the intersection
    val start = maxOf(segment1.first, segment2.first)
    val end = minOf(segment1.second, segment2.second)

    return Pair(start, end)
}
/**
 * Given two segments represented as pairs of integers, segment1 and segment2,
 * returns the intersection of the segments if it exists, otherwise returns null.
 *
 * @param segment1 A pair representing the first segment as (start1, end1)
 * @param segment2 A pair representing the second segment as (start2, end2)
 * @return A pair representing the intersection of the segments as (start, end), or null if no intersection.
 *
 * Example:
 * intersectionOfSegments(Pair(1, 5), Pair(4, 7)) // Output: Pair(4, 5)
 * intersectionOfSegments(Pair(-100, 0), Pair(100, 200)) // Output: null
 */
fun intersectionOfSegments(segment1: Pair<Int, Int>, segment2: Pair<Int, Int>): Pair<Int, Int>? {
/**
 * Given two segments represented as pairs of integers, segment1 and segment2,
 * returns the intersection of the segments if it exists, otherwise returns null.
 *
 * @param segment1 A pair representing the first segment as (start1, end1)
 * @param segment2 A pair representing the second segment as (start2, end2)
 * @return A pair representing the intersection of the segments as (start, end), or null if no intersection.
 *
 * Example:
 * intersectionOfSegments(Pair(1, 5), Pair(4, 7)) // Output: Pair(4, 5)
 * intersectionOfSegments(Pair(-100, 0), Pair(100, 200)) // Output: null
 */
fun intersectionOfSegments(segment1: Pair<Int, Int>, segment2: Pair<Int, Int>): Pair<Int, Int>? {

    // Check if there is any overlap between the segments
    if (segment1.first > segment2.second || segment2.first > segment1.second) {
        return null
    }

    // Determine the start and end values of the intersection
    val start = maxOf(segment1.first, segment2.first)
    val end = minOf(segment1.second, segment2.second)

    return Pair(start, end)
}