private boolean isBalanced() {
        int min = assignmentManager.targetAssignmentSize(sortedMembersByAssignmentSize.first());
        int max = assignmentManager.targetAssignmentSize(sortedMembersByAssignmentSize.last());

        // If minimum and maximum numbers of partitions assigned to members differ by at most one return true.
        if (min >= max - 1)
            return true;

        // Ensure that members without a complete set of topic partitions cannot receive any additional partitions.
        // This maintains balance. Start by checking members with the fewest assigned partitions to see if they can take more.
        for (String member : sortedMembersByAssignmentSize) {
            int memberPartitionCount = assignmentManager.targetAssignmentSize(member);

            // Skip if this member already has all the topic partitions it can get.
            int maxAssignmentSize = assignmentManager.maxAssignmentSize(member);
            if (memberPartitionCount == maxAssignmentSize)
                continue;

            // Otherwise make sure it cannot get any more partitions.
            for (Uuid topicId : groupSpec.memberSubscription(member).subscribedTopicIds()) {
                Set<Integer> assignedPartitions = targetAssignment.get(member).partitions().get(topicId);
                for (int i = 0; i < subscribedTopicDescriber.numPartitions(topicId); i++) {
                    TopicIdPartition topicIdPartition = new TopicIdPartition(topicId, i);
                    if (assignedPartitions == null || !assignedPartitions.contains(i)) {
                        String otherMember = partitionOwnerInTargetAssignment.get(topicIdPartition);
                        int otherMemberPartitionCount = assignmentManager.targetAssignmentSize(otherMember);
                        if (memberPartitionCount + 1 < otherMemberPartitionCount) {
                            LOG.debug("{} can be moved from member {} to member {} for a more balanced assignment.",
                                topicIdPartition, otherMember, member);
                            return false;
                        }
                    }
                }
            }
        }
        return true;
    }
Checks if the current assignments of partitions to members is balanced.

Balance is determined by first checking if the difference in the number of partitions assigned
to any two members is one. If this is not true, it verifies that no member can
receive additional partitions without disrupting the balance.

@return true if the assignment is balanced, false otherwise.
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/assignor/UniformHeterogeneousAssignmentBuilder.java