def _balance_partitions(x, N_partitions, N_elements):
    """Balances out the number of elements across the partitions"""
    n_per_part = N_elements / N_partitions / 2
    part = 0
    if x > N_elements / 2:
        x = N_elements - x
    part = x / n_per_part
    return part if part < N_partitions else part - 1
