Minimum absolute difference of server loads | Function which returns the minimum difference of loads ; Compute the overall server load ; Stores the results of subproblems ; Fill the partition table in bottom up manner ; If i - th server is included ; If i - th server is excluded ; Server A load : total_sum - ans Server B load : ans Diff : abs ( total_sum - 2 * ans ) ; Driver Code ; Function Call
def minServerLoads ( n , servers ) : NEW_LINE INDENT totalLoad = sum ( servers ) NEW_LINE requiredLoad = totalLoad // 2 NEW_LINE dp = [ [ 0 for col in range ( requiredLoad + 1 ) ] for row in range ( n + 1 ) ] NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , requiredLoad + 1 ) : NEW_LINE INDENT if servers [ i - 1 ] > j : NEW_LINE INDENT dp [ i ] [ j ] = dp [ i - 1 ] [ j ] NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] = max ( dp [ i - 1 ] [ j ] , servers [ i - 1 ] + dp [ i - 1 ] [ j - servers [ i - 1 ] ] ) NEW_LINE DEDENT DEDENT DEDENT return totalLoad - 2 * dp [ n ] [ requiredLoad ] NEW_LINE DEDENT N = 5 ; NEW_LINE servers = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE print ( minServerLoads ( N , servers ) ) NEW_LINE