This multi-part exercise evaluates algorithmic problem-solving across combinatorics and geometry (counting axis-aligned square subgrids), string reordering under adjacency constraints (minimum adjacent swaps to form a palindrome), deterministic grid-walk simulation with teleportation and cycle detection, and prefix-sum based range optimization for arrays, and it belongs to the Coding & Algorithms domain. These problems are commonly asked to assess efficiency under large constraints, correctness in handling edge cases and invariants, and the ability to blend conceptual understanding with practical algorithm design, requiring both conceptual reasoning and applied implementation skills.
You are given four independent coding questions.
For each query, you are given two integers R and C representing an R × C grid of unit cells.
Return the total number of axis-aligned square subgrids (of all possible side lengths) that can be formed inside the grid.
q
queries, each query contains
R, C
.
1 ≤ q ≤ 2e5
,
1 ≤ R, C ≤ 1e9
.
You are given a string s of length n (in the OA it may be a binary string such as only 'O' and 'I', but assume it can be any characters).
In one move, you may swap two adjacent characters.
Return the minimum number of adjacent swaps needed to rearrange s into a palindrome.
-1
.
You are given an n × m grid. You start at the top-left cell (0,0) and want to reach the bottom-right cell (n-1,m-1).
Movement rule (row-major walk):
(r,c)
, your next step is normally:
(r, c+1)
if
c+1 < m
(r+1, 0)
if
r+1 < n
Additional rules:
-1
.
-2
.
Return the number of steps to reach (n-1,m-1) if reachable.
You are given an array a of length n (assume a[i] ≥ 0) and an integer k.
Let prefix sums be:
pref[0] = 0
pref[i+1] = pref[i] + a[i]
Find the maximum length of a subarray [l, r) (with 0 ≤ l < r ≤ n) such that:
Return this maximum length.