S1Q3 · Divide Number Into Almost Equal Parts¶
⚡ Quick Reference
Function: divide_into_almost_equal_parts(n: int, k: int) -> list
Core idea: base value is n // k, remainder n % k elements get one extra -place larger values first.
def divide_into_almost_equal_parts(n: int, k: int) -> list:
base, remainder = divmod(n, k)
return [base + 1] * remainder + [base] * (k - remainder)
Key rules:
- base = n // k -minimum value each part gets
- remainder = n % k -how many parts get one extra
- Larger parts (base + 1) come first
- Total always sums to n: remainder*(base+1) + (k-remainder)*base = n
Problem Statement¶
Problem
Write a function divide_into_almost_equal_parts(n, k) that returns a list of k integers that sum to n, are as equal as possible, and have larger values at the beginning.
Examples:
n=5, k=3
[2, 2, 1]
n=16, k=3
[6, 5, 5]
n=12, k=4
[3, 3, 3, 3]
n=10, k=3
[4, 3, 3]
Understanding the problem¶
When dividing n into k parts, integer division leaves a remainder:
- Every part gets at least
n // k - The leftover
n % kunits are distributed one each to the firstn % kparts - Those parts end up with
n // k + 1-making them the "larger" values at the front
divmod(n, k)
divmod(n, k) returns (n // k, n % k) in a single call -the base value and the remainder together. Cleaner than computing them separately.
Tracing all examples¶
n |
k |
base = n//k |
remainder = n%k |
Result |
|---|---|---|---|---|
| 5 | 3 | 1 | 2 → first 2 get +1 | [2, 2, 1] |
| 16 | 3 | 5 | 1 → first 1 gets +1 | [6, 5, 5] |
| 12 | 4 | 3 | 0 → no extras | [3, 3, 3, 3] |
| 10 | 3 | 3 | 1 → first 1 gets +1 | [4, 3, 3] |
Verify n=5, k=3:
[base+1]*remainder + [base]*(k-remainder)
= [2]*2 + [1]*1
= [2, 2, 1] -sum = 5
Solution approaches¶
def divide_into_almost_equal_parts(n: int, k: int) -> list:
base, remainder = divmod(n, k)
return [base + 1] * remainder + [base] * (k - remainder)
One line of logic after divmod. [base+1] * remainder creates the larger elements, [base] * (k-remainder) fills the rest.
def divide_into_almost_equal_parts(n: int, k: int) -> list:
base = n // k
remainder = n % k
result = []
for i in range(k):
if i < remainder:
result.append(base + 1) # first 'remainder' parts get extra
else:
result.append(base)
return result
Explicit loop -the condition i < remainder decides whether each part gets the bonus unit.
def divide_into_almost_equal_parts(n: int, k: int) -> list:
base, remainder = divmod(n, k)
return [base + (1 if i < remainder else 0) for i in range(k)]
Single comprehension -adds 1 to the first remainder elements, 0 to the rest.
def divide_into_almost_equal_parts(n: int, k: int) -> list:
base, remainder = divmod(n, k)
return list(map(lambda i: base + (1 if i < remainder else 0), range(k)))
map(lambda i: ..., range(k)) applies the same logic as the comprehension but in functional style. Each index i is mapped to its part value.
Key takeaways¶
divmod(n, k) for quotient and remainder
divmod(n, k) returns (n // k, n % k) in one call. Use it whenever you need both -avoids computing the same division twice.
[val] * count for repeated lists
[x] * n creates a list of n copies of x. Concatenating two such lists ([a]*r + [b]*(k-r)) is the most compact way to build the result.
Remainder distributes the extras
The remainder tells you exactly how many parts get one extra unit. First remainder parts get base+1, the rest get base. Sum always equals n.