
The first line of input contains an integer ‘T’, denoting the number of test cases.
The first line of each test case contains two spaced integers ‘N’ and ‘K’, denoting the number of candies and the integer ‘K’.
The following line contains an array ‘A’ of ‘N’ spaced integers denoting the sweetness value of each candy.
For each test case, print three single-spaced integers in a new line denoting the starting indexes(0 based) of all the 3 intervals.
You are not required to print the expected output. It has already been taken care of. Just implement the function.
1 <= T <= 10
1 <= N <= 2 * 10^4
1 <= A[i] <= 2^16
1 <= K <= floor(N / 3)
Time Limit: 1 sec
Basically, we need to find three non-overlapping subarrays such that the sum of these three subarrays is maximum. We can use three nested loops to try to find all possible ways to select non-overlapping subarrays.
To find the sum of the subarray quickly we will use the prefix sum.
Let’s create another array ‘W’ where ‘W[i]’ denotes the sum of all the elements from ‘i’ to ‘i’ + ‘K’ - 1.
We can do this by using prefix sums or managing the sum of the interval as a window slides along the array.
Now we need to find ‘i’, ‘j’, ‘l’ such that ‘i’ + ‘K’ <= ‘j’ , ‘j’ + ‘K’ <= ‘l’ and sum of ‘W[i]’ + ‘W[j]’ + ‘W[l]’ is maximum.
If we fix ‘j’, then we need to final the smallest ‘i’ from the range 0 to ‘j’ - ‘K’ and the smallest ‘l’ from the range ‘j’ + ‘K’ to ‘N’.
We can solve these problems with dynamic programming.
Let’s create an array ‘left’ where ‘left[i]’ denotes the smallest index of the best subarray we can select from 0 to ‘i’.
Similarly, we can create an array ‘right’ where ‘right[i]’ denotes the smallest index of the best subarray we can select from ‘i’ to ‘N’ - 1.
Thus, in the end for every ‘j’ we know the best ‘i’ is present in ‘left[j - K]’ and best ‘l’ is present in ‘right[j + K]’.
Count vowels, consonants, and spaces
King Placement
No Repeated Digits
No Repeated Digits
Smit’s String Flip Game.
Longest Alternating Subarray