def next_k_array(a):
    """
    Given an array `a` of k distinct nonnegative integers, sorted in
    ascending order, return the next k-array in the lexicographic
    ordering of the descending sequences of the elements [1]_. `a` is
    modified in place.

    Parameters
    ----------
    a : ndarray(int, ndim=1)
        Array of length k.

    Returns
    -------
    a : ndarray(int, ndim=1)
        View of `a`.

    Examples
    --------
    Enumerate all the subsets with k elements of the set {0, ..., n-1}.

    >>> n, k = 4, 2
    >>> a = np.arange(k)
    >>> while a[-1] < n:
    ...     print(a)
    ...     a = next_k_array(a)
    ...
    [0 1]
    [0 2]
    [1 2]
    [0 3]
    [1 3]
    [2 3]

    References
    ----------
    .. [1] `Combinatorial number system
       <https://en.wikipedia.org/wiki/Combinatorial_number_system>`_,
       Wikipedia.

    """
    # Logic taken from Algotirhm T in D. Knuth, The Art of Computer
    # Programming, Section 7.2.1.3 "Generating All Combinations".
    k = len(a)
    if k == 1 or a[0] + 1 < a[1]:
        a[0] += 1
        return a

    a[0] = 0
    i = 1
    x = a[i] + 1

    while i < k-1 and x == a[i+1]:
        i += 1
        a[i-1] = i - 1
        x = a[i] + 1
    a[i] = x

    return a
Given an array `a` of k distinct nonnegative integers, sorted in
    ascending order, return the next k-array in the lexicographic
    ordering of the descending sequences of the elements [1]_. `a` is
    modified in place.

    Parameters
    ----------
    a : ndarray(int, ndim=1)
        Array of length k.

    Returns
    -------
    a : ndarray(int, ndim=1)
        View of `a`.

    Examples
    --------
    Enumerate all the subsets with k elements of the set {0, ..., n-1}.

    >>> n, k = 4, 2
    >>> a = np.arange(k)
    >>> while a[-1] < n:
    ...     print(a)
    ...     a = next_k_array(a)
    ...
    [0 1]
    [0 2]
    [1 2]
    [0 3]
    [1 3]
    [2 3]

    References
    ----------
    .. [1] `Combinatorial number system
       <https://en.wikipedia.org/wiki/Combinatorial_number_system>`_,
       Wikipedia.