/**
 * Given a specific range, iterate over the range and perform appending and removing elements from a list.
 *
 * @param start The starting value of the range (inclusive).
 * @param end The ending value of the range (exclusive).
 * @param elements The initial list of elements.
 * @return The modified list after performing appending and removing actions.
 */
fun iterateAndModifyList(start: Int, end: Int, elements: MutableList<Int>): MutableList<Int> {

    val result = elements.toMutableList()  // Create a copy of the original list to avoid modifying it directly

    for (i in start until end) {
        // Append the square of 'i' to the list
        result.add(i * i)

        // Remove the element at index 'i' if it exists
        if (result.size > i) {
            result.removeAt(i)
        }
    }

    return result
}
/**
 * Given a specific range, iterate over the range and perform appending and removing elements from a list.
 *
 * @param start The starting value of the range (inclusive).
 * @param end The ending value of the range (exclusive).
 * @param elements The initial list of elements.
 * @return The modified list after performing appending and removing actions.
 */
fun iterateAndModifyList(start: Int, end: Int, elements: MutableList<Int>): MutableList<Int> {
/**
 * Given a specific range, iterate over the range and perform appending and removing elements from a list.
 *
 * @param start The starting value of the range (inclusive).
 * @param end The ending value of the range (exclusive).
 * @param elements The initial list of elements.
 * @return The modified list after performing appending and removing actions.
 */
fun iterateAndModifyList(start: Int, end: Int, elements: MutableList<Int>): MutableList<Int> {

    val result = elements.toMutableList()  // Create a copy of the original list to avoid modifying it directly

    for (i in start until end) {
        // Append the square of 'i' to the list
        result.add(i * i)

        // Remove the element at index 'i' if it exists
        if (result.size > i) {
            result.removeAt(i)
        }
    }

    return result
}