Drill

ProblemsC++ › network

How many pages does the result set need

easynetworkMathC++

A paginated API needs to know how many pages to render. Divide the total items by the page size and round up.

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.

Solve it in Python →

Where you start

int pagesTotal(int items, int perPage) {
    
}

Worked examples

CallResult
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;
}

The same problem in another language

More network problems in C++