hackercup / 2022 /round1 /consecutive_cuts_ch1_sol.md
wjomlex's picture
2022 Problems
f7ba5f2 verified

When (K = 0), we can't perform any cuts, so the answer is YES if and only if (A = B).

When (K = 1), first note that the answer is NO if (A = B), since we we are forced to disrupt the equality with a cut (which cannot be (0) cards). Otherwise if (A \ne B), we see that cutting the deck once is the same as rotating an array. Checking if a sorted array (A) has been rotated to (B) can be done by checking that each pair of adjacent values in (B) are in order, with exactly one exception. Since (A) is not guaranteed to be sorted, we can precompute a mapping of (A) values to their indices, and instead check that the mapped indices of all (B) values are in order, with exactly one exception.

When (K \ge 2), we can observe that any sequence of rotations that doesn't preserve the original order can be replicated with just one rotation. Again, we can just output YES if (B) is a rotation of (A). If (A = B), we can also output YES, as it only takes one rotation to restore any rotated deck back to the original order.

A special case to watch out for is when (N = 2), e.g. (A = [1, 2]). When (B = [1, 2]), the answer is YES if and only if (K) is even. When (B = [2, 1]), then answer is YES if and only if (K) is odd. The answers are flipped if (A = [2, 1]).

See David Harmeyer's solution video here.