Skip to content
DeepForgeby svx
All problems
MediumData Structuresds-320

Mo's Algorithm Block Size

Problem

A standard Mo's algorithm block size is n / sqrt(q), which balances left-pointer and right-pointer movement. Using integer arithmetic, block_size = max(1, n // max(1, isqrt(q))) and block_count = ceil(n / block_size). Return [block_size, block_count], or [0, 0] when n is 0.

Hint

When q is 0 or 1, isqrt gives at most 1, so the block size collapses to n.

Test cases

case 1
in: [100,100]
expected: [10,10]
case 2
in: [10,3]
expected: [10,1]
case 3
in: [17,4]
expected: [8,3]
case 4
in: [5,0]
expected: [5,1]
Starter codePython
def mo_block_size(n, q):
    # Your code here
    pass

4 test cases · run your code against them in the editor