def find_valid_edges(components, valid_edges):
    """Find all edges between two components in a complete undirected graph.

    Args:
        components: A [V]-shaped array of boolean component ids. This assumes
            there are exactly two nonemtpy components.
        valid_edges: An uninitialized array where output is written. On return,
            the subarray valid_edges[:end] will contain edge ids k for all
            valid edges.

    Returns:
        The number of valid edges found.
    """
    k = 0
    end = 0
    for v2, c2 in enumerate(components):
        for v1 in range(v2):
            if c2 ^ components[v1]:
                valid_edges[end] = k
                end += 1
            k += 1
    return end