def find_out_of_order_packet_indices(packet_ns):
    """
    Return indices of packets which have apparently arrived out-of-order.
    Specifically: return indices of any packet number which was less than the
    previous packet number. For example, for the list of packet numbers:
        0, 1, 2, 3, 5, 4, 6, 7.
    return index 5 (corresponding to '4').
    """
    indices = []
    prev_packet_n = packet_ns[0] - 1
    for i in range(len(packet_ns)):
        packet_n = packet_ns[i]
        if packet_n > prev_packet_n:
            # say, if the previous packet was 2, and now we're at 3
            # or if the previous packet was 3, and now we're at 5
            prev_packet_n = packet_n
        elif packet_n < prev_packet_n:
            # e.g. if the previous packet was 5, and we're now at 4
            # (having given up all hope of seeing 4 again when we saw 5)
            indices.append(i)
    return indices