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
Let's break the possible values of m (the uniqueness value) into ranges within [n, 3n - 3]:
Case 1: m == n
- We only need the n single-element subarrays, each trivially distinct.
- Output any array with all equal elements like
[1, 1, 1, ..., 1]. - This ensures no subarray of length > 1 has all distinct elements.
- Total uniqueness value:
n.
Case 2: n < m < 2n
- We already get $$$n$$$ uniqueness from 1-element subarrays.
- We now need $$${m - n}$$$ more distinct subarrays of length 2.
- Example: $$$[1, 2, 1, 2]$$$
Subarrays with indices: $$$[0,1]$$$, $$$[1,2]$$$, $$$[2,3]$$$ contribute 1 to the uniqueness → 3 subarrays of length 2 with distinct elements. - In general, we can form
n - 1such subarrays in an array of sizen. - Hence, we can reach up to
m = 2n - 1using only1and2.
Case 3: m ≥ 2n
- We introduce the third element
3to pushf(a)further. - Similar construction: start with
[1, 2, 3], then alternate like[1,2,3,1,2,...]to get more distinct subarrays. - Try analyzing how many new unique subarrays we get of lengths 2 and 3.
- (Try to prove this part yourself): The maximum uniqueness we can achieve is
3n - 3.
600816E - Mukesh's Love for Sweets
Mukesh wants to maximize the number of sweets he can get, which is determined by the minimum marks he can achieve across 3 subjects (Physics, Chemistry, and Mathematics) within a given time interval T. We need to maximize the minimum marks and that's where Binary Search comes in!
We will precompute the maximum achievable marks for each subject, considering different total times. We use DP to calculate the maximum marks obtainable within a given time limit for each subject. This is similar to the knapsack problem where we are trying to maximize the total marks subject to a time constraint. For each subject, we construct a DP array:dp[i] = minimum time required to get exactly i marks
We then use this DP array to track the minimum time needed for any given number of marks (from 0 to the maximum possible marks).
Now for each query, we need to find the maximum number of marks x such that the total time to achieve x marks in each subject does not exceed the given time limit T and maximize it if its possible.
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.
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 fromd_nodeto 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 logic can be used efficiently using depth arrays and parent arrays after a DFS traversal of the tree.




