Drill

ProblemsGo › finance

Split a total into installments

hardfinanceMathArraysGreedyGo

A checkout spreads a total across installments, keeping the early ones a cent higher so each slice is as equal as possible.

installmentPlan(totalMinor: int, installments: int) → list<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 installmentPlan(totalMinor int, installments int) []int {
	
}

Worked examples

CallResult
installmentPlan(100, 3)[]int{34, 33, 33}
installmentPlan(100, 6)[]int{17, 17, 17, 17, 16, 16}
installmentPlan(7, 3)[]int{3, 2, 2}
installmentPlan(10, 2)[]int{5, 5}

Hint

Compute the floor share and the leftover, then hand the leftovers to the front.

Reference solution in Go
func installmentPlan(totalMinor int, installments int) []int {
	result := []int{}
	if installments <= 0 {
		return result
	}
	per := totalMinor / installments
	extra := totalMinor % installments
	for i := 0; i < installments; i++ {
		if i < extra {
			result = append(result, per+1)
		} else {
			result = append(result, per)
		}
	}
	return result
}

The same problem in another language

More finance problems in Go