How many pages does the result set need
A paginated API needs to know how many pages to render. Divide the total items by the page size and round up.
- Return ceil(items / perPage) using integer arithmetic.
- If items is zero or negative, return 0.
- If perPage is zero or negative, return 0.
pagesTotal(items: int, perPage: int) → int
C++ 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.
Where you start
int pagesTotal(int items, int perPage) {
}
Worked examples
| Call | Result |
|---|---|
pagesTotal(10, 3) | 4 |
pagesTotal(9, 3) | 3 |
pagesTotal(0, 5) | 0 |
pagesTotal(5, 0) | 0 |
Hint
The classic integer ceil is (items + perPage - 1) / perPage.
Reference solution in C++
int pagesTotal(int items, int perPage) {
if (items <= 0 || perPage <= 0) return 0;
return (items + perPage - 1) / perPage;
}