code: def get_replication_group_imbalance_stats(rgs, partitions):
      
      tot_rgs = len(rgs)
      extra_replica_cnt_per_rg = defaultdict(int)
      for partition in partitions:
          # Get optimal replica-count for each partition
          opt_replica_cnt, extra_replicas_allowed = \
              compute_optimum(tot_rgs, partition.replication_factor)
  
          # Extra replica count for each rg
          for rg in rgs:
              replica_cnt_rg = rg.count_replica(partition)
              extra_replica_cnt, extra_replicas_allowed = \
                  get_extra_element_count(
                      replica_cnt_rg,
                      opt_replica_cnt,
                      extra_replicas_allowed,
                  )
              extra_replica_cnt_per_rg[rg.id] += extra_replica_cnt
  
      # Evaluate net imbalance across all replication-groups
      net_imbalance = sum(extra_replica_cnt_per_rg.values())
      return net_imbalance, extra_replica_cnt_per_rg
docs: Calculate extra replica count replica count over each replication-group
      and net extra-same-replica count.