Problems > All the Squares > Editorial

All the Squares — Solution & Editorial

Back to the Problem

Size-i squares anchor at (N−i+1)(M−i+1) positions; sum over sizes (a closed form exists via square-sum identities, but the O(min) loop is instant).

Complexity: O(min(N, M))