def kth_element(arr, n, k):
    if n <= 0 or k < 1 or k > n:
        raise IndexError("k is out of bounds")
    return sorted(arr[:n])[k - 1]