Drill

ProblemsGo › billing

How much is still owed

easybillingArraysMathGo

Compare the amount due against a list of payments already received and return the shortfall.

balanceShortfall(due: int, payments: list<int>) → int

Go needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

func balanceShortfall(due int, payments []int) int {
	
}

Worked examples

CallResult
balanceShortfall(10000, []int{3000, 4000})3000
balanceShortfall(5000, []int{5000})0
balanceShortfall(5000, []int{6000})0
balanceShortfall(1000, []int{})1000

Hint

Sum the payments and subtract from due.

Reference solution in Go
func balanceShortfall(due int, payments []int) int {
	paid := 0
	for _, p := range payments {
		paid += p
	}
	if due > paid {
		return due - paid
	}
	return 0
}

The same problem in another language

More billing problems in Go