Here are the hints for Week 6 of the DSA Learning Series.
We can prove that only 3 adjacent coders can be chosen. Let index k be the index of the middle coder. One coder has to have index >=(k+1) and the other has to have index <=(k-1). If we choose a coder with index k+2, then the difference in indices between the two corner coders will be >2. Therefore, those two will not agree to be in the same team.
Since we only have to check 3 adjacent friends, we can write a brute force function for this. Iterate over all i representing the index of the middle friend. Take max of current_ans and a(i) + a(i-1) + a(i+1).
In order to account for the cyclic nature of the array, take a(i-1) as a((i-1+n)%n) and a(i+1) as a((i+1)%n).
Prefix_sum(i) + Suffix_sum(i) can be written as a(1) + a(2) + … + a(i) + a(i) + a(i+1) + a(i+2) + … + a(n). Therefore, prefix_sum(i) + suffix_sum(i) = sum_of_array + a(i).
In order to minimise sum_of_array + a(i), we need to only minimise a(i) because sum_of_array is fixed over all indices. Therefore, the problem can be reduced to “output the index of the smallest element in the array”. The reduced problem is trivial.
The expected complexity is B.log(Y)
Since B is <=700, we can iterate over all possibilities for B. Now, we need to find the number of distinct integers A such that A^2 <= (Y - B)
If K^2 <= (Y-B) then (K-1)^2 <= (Y-B). Therefore, we simple need to find the greatest integer A such that A^2 <= (Y-B).
You can perform the above task using binary search.
The expected complexity is O(N)
This problem is just some casework. There are 4 cases we need to check for:
When the answer is just the maximum sum present inside a single array of size N.
-1e9 5 -1e9
The answer in this case will be 5
When the answer is the sum of the entire array, K times.
3 3 3
The answer in this case will be 27
When the answer is the maximum suffix of one array and the maximum prefix of the next.
4 -1e9 -1e9 4
The answer in this case will be 4 + 4 = 8
When the answer is the maximum suffix of the first array + the maximum prefix of the last array + all of the arrays in the middle
3 -5 3
The answer in this case will be 8.
You can find the maximum suffix and prefix by doing linear scans from both sides and taking max. You can find the maximum sum present in the array using Kadane’s algorithm.
If any subsequence of K students contains no student from the same batch, it means that the students’ batches are repeating with period of K.
In order to minimise the eating capacity, start greedily assigning batches from 0 to K-1.
Eg. If K = 3 and N = 7
0 1 2 0 1 2 0
Now, the answer can be expressed as (sum of first K fibonacci numbers) x (n/k) + (sum of first (n%k) fibonacci numbers)
If you attempt to find the sum of the first N fibonacci numbers in O(N), you will get TLE. Use matrix exponentiation to find the sum in O(LogN). Learn about this technique here:
Our first step should be to optimise the process of finding count of 1s and 0s in a range. This can be achieved using RMQ via prefix sums.
Now, let’s analyse the nature of the length of the special sequences. If cnt0 = cnt1 ^ 2 then the length of the sequence is cnt1^2 + cnt1
Therefore, we need only check sequences that can be expressed in the form x^2 + x where (x^2 + x) <= N
Now, for each x (as described above), check each substring of that length for whether it is a special substring.
Let’s look at this expression:
(Sum of kC2 from k = 2 to k = d)*V(i)
kC2 is the sum of the first (K-1) positive integers
Therefore, the expression is really this:
(Sum of (Sum of first k integers) for all k from 1 to (d-1)) x V(i)
If we take repeated prefix sums, we can achieve the above expression. Let me give an idea of this. Consider the following population array:
[V, 0, 0, 0]
Let’s take prefix sums one time
[V, V, V, V]
Now, all array positions have coefficient 1 (the first natural number).
If we take prefix sums again, the coefficient at position i should be i
[V, 2V, 3V, 4V]
Now, if we take prefix sums yet again, the coefficient at position i will be the sum of the first i natural numbers
[V, 3V, 6V, 10V]
Finally, if we take prefix sums yet again, the coefficient at position i will be the sum of (sum of the first k natural numbers) for each k from 1 to i
[V, 4V, 10V, 20V]
Therefore, we have to take the prefix sums 4 times.
Now that we have the prefix sums, we can repeat this process for suffix sums.
For each i, the answer works out to pref[i-1] + suff[i+1]
We only need to consider the array between index 1 and X, the part after it becomes irrelevant. Therefore, if we set N to X, we need to only find A[N] after M prefix sums operations.
Look at this pattern of doing performing prefix sums repeatedly:
[A, B, C, D]
[A, A + B, A + B + C, A + B + C + D]
[A, 2A + B, 3A + 2B + C, 4A + 3B + 2C + D]
[A, 3A + B, 6A + 3B + C, 10A + 6B + 3C + D]
[A, 4A + B, 10A + 4B + C, 20A + 10B + 4C + D]
[A, 5A + B, 15A + 5B + C, 35A + 15B + 5C + D]
Look at A(N) after 5 operations:
35A + 15B + 5C + D
Let’s look at the coefficients of each element.
D has coefficient 1 = 4C0
C has coefficient 5 = 5C1
B has coefficient 15 = 6C2
A has coefficient 35 = 7C3
Using this pattern, it’s simple to calculate A(N) in O(N.LogN)
In order to calculate nCr, pre-calculate the values of each factorial and the modular multiplicative inverse of each factorial.