600816A - Pass The Baton
Suppose the total number of lines of code is a multiple of 3, i.e., of the form $$$3n$$$. Then the work can be evenly distributed among the three members — each writing exactly $$$n$$$ lines of code
If the number of lines is of the form $$$3n + 1$$$, then the optimal distribution would be: $$${n,\ n,\ n + 1}$$$
If the number of lines is of the form $$$3n + 2$$$, then the optimal distribution would be: $$${n,\ n + 1,\ n + 1}$$$
In both uneven cases above, the maximum number of lines any one member has to write is still $$${n + 1}$$$. Thus, for all three forms $$${3n,\ 3n + 1,\ 3n + 2}$$$, the answer is always: $$$\lceil \frac{x}{3} \rceil$$$ where $$$\lceil . \rceil$$$ is the ceiling value, and $$$x$$$ is the total number of lines of code.
600816B - Where's The Baton?
Just stimulate the process: Iterate from index 0 to n-1, maintain a pointer j to the string "baton" initially at index 0. For each character in the input string, if it matches the current character pointed to in "baton", move the pointer forward. If the pointer reaches the end of "baton" j == 5, that means all characters 'b', 'a', 't', 'o', 'n' have been found in the same order one after another.
600816C - Matrix Swap
Since it is guaranteed that some combination of up to one row swap and one column swap will convert A into B, you just need to find two rows (if such row exists) which are "Not Equivalent" in both A and B. This means that these rows were swapped in B to get A.
"Not Equivalent" means that the set of elements in the i-th row of A is not the same as the set of elements in the i-th row of B.
1, 2, 3, 4, 5 is "Equivalent" to 1, 4, 3, 2, 5 (both have the same elements)1, 2, 3, 4, 5 is "Not Equivalent" to 5, 6, 7, 8, 9 (both sets are disjoint)
So we can sort A[i] and B[i] and compare them!
If they are not equal, then obviously the i-th row of A has been swapped with some j-th row in A such that A[j] = B[i]. Find that index j and output i and j (The two rows that were swapped)
Similarly, do the same stuff for columns!
600816D - Uniqueness in Three
600816E - Mukesh's Love for Sweets
600816F - Samrat's Algorithm
To approach the problem effectively, let us analyze each bit position independently. For every bit index i from 0 to 29 (assuming the maximum element fits within 30 bits), define cnt[i] as the number of elements in the array a that have the i-th bit set. The bits are considered from the least significant bit (0-th) upwards. Computing this array of counts requires O(30 * n) time, or more generally O(n * log(max(ai))) operations.
Now, consider how the score is calculated:
While there exists at least one non-zero element in the array, we perform the following operations: - Count how many elements are odd, i.e., have the 0-th bit set. - Add that count to the score. - Right shift each element in the array by one position (equivalent to dividing by 2 and taking the floor).
This process continues until all elements reduce to zero. If we track how many times a 1 appears at each bit position throughout the execution, the total score is simply the sum of all cnt[i] for i in [0, 29].
Our objective is to minimize this total score, and we are allowed to do so by selecting a single integer x to XOR with every element in the array before the process starts.
Here’s the key idea:
- There are
nelements in total, so for each biti, the maximum possible value ofcnt[i]isn. - If
cnt[i] > n / 2, it means the majority of elements have a 1 at that bit. - To reduce the number of 1s at that bit, we can flip them by setting bit
iinxto 1. - This effectively inverts all bits at that position, changing the count of 1s to
n - cnt[i]. - If
cnt[i] <= n / 2, the 0s are already in the majority. - Flipping would increase the number of 1s, so we leave bit
iinxas 0 and keepcnt[i]unchanged.
By applying this logic across all bit positions, we construct the integer x that minimizes the total contribution of set bits across all elements and all positions.
Finally, after applying XOR with x, we recalculate the modified cnt[i] values and sum them up.
This sum gives us the minimum achievable score, and the corresponding x is the optimal number to apply the XOR transformation with.
600816G - Bob's Valentine's Day
Suppose we are given a list of nodes: u₁, u₂, ..., uₘ.
We want to check whether all these nodes lie on a single path from the root (vertex 1) to some vertex v, such that depth[v] ≥ max{depth[u₁, u₂, ..., uₘ]}.
Observation:
If the given nodes lie on such a path, then their parents must also lie on the same path from root to v.
Approach:
- First, find the node with the maximum depth among the list — call it d_node. - This d_node will serve as our candidate v (the bottom of the path). - Now, traverse from d_node upward to the root (vertex 1) and mark all nodes on this path. - Then, for every node uᵢ in the list: — If uᵢ == 1 (the root), you can skip checking — it's always part of the path. — Otherwise, check whether the parent of uᵢ lies on the upward path from d_node to root. — If any such parent is not found in this path, then the list of nodes does not lie on a valid single path from root to v.
This method ensures that all the nodes are connected through parent pointers along a single upward path from the deepest node back to the root.
This logic can be used efficiently using depth arrays and parent arrays after a DFS traversal of the tree.




