/**
 * This function takes in two lists and finds the common elements between them using slicing.
 *
 * @param list1 A list of elements.
 * @param list2 A list of elements.
 * @return A list of elements that are common between list1 and list2.
 */
fun findCommonElements(list1: List<Int>, list2: List<Int>): List<Int> {

    val commonElements = mutableListOf<Int>()

    val sortedList1 = list1.sorted()
    val sortedList2 = list2.sorted()

    var i = 0
    var j = 0

    while (i < sortedList1.size && j < sortedList2.size) {
        when {
            sortedList1[i] == sortedList2[j] -> {
                commonElements.add(sortedList1[i])
                i++
                j++
            }
            sortedList1[i] < sortedList2[j] -> i++
            else -> j++
        }
    }

    return commonElements
}
/**
 * This function takes in two lists and finds the common elements between them using slicing.
 *
 * @param list1 A list of elements.
 * @param list2 A list of elements.
 * @return A list of elements that are common between list1 and list2.
 */
fun findCommonElements(list1: List<Int>, list2: List<Int>): List<Int> {
/**
 * This function takes in two lists and finds the common elements between them using slicing.
 *
 * @param list1 A list of elements.
 * @param list2 A list of elements.
 * @return A list of elements that are common between list1 and list2.
 */
fun findCommonElements(list1: List<Int>, list2: List<Int>): List<Int> {

    val commonElements = mutableListOf<Int>()

    val sortedList1 = list1.sorted()
    val sortedList2 = list2.sorted()

    var i = 0
    var j = 0

    while (i < sortedList1.size && j < sortedList2.size) {
        when {
            sortedList1[i] == sortedList2[j] -> {
                commonElements.add(sortedList1[i])
                i++
                j++
            }
            sortedList1[i] < sortedList2[j] -> i++
            else -> j++
        }
    }

    return commonElements
}