Maximum subarray sum after K concatenation
The first line of input contains an integer 'T' representing the number of test cases or queries to be processed. Then the test case follows. The first line of each test case contains two single space-separated integers ‘N’ and ‘K’ representing the size of the vector/list and the given integer, respectively. The second line of each test case contains ‘N’ single space-separated integers representing the vector elements.
For each test case, print an integer denoting the maximum possible subarray sum of 'CONCAT'. Print the output of each test case in a separate line.
You do not need to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 100 1 <= N <= 5*10^3 1 <= K <= 5*10^3 -10^5 <= ARR[i] <= 10^5 Time Limit: 1sec
The main observation here is that the optimal subarray will have no prefix with negative sum. This is because we can remove the prefix with a negative sum from our optimal subarray and hence it will only increase our subarray sum/answer.
Please note, we don’t need to construct a new vector ‘CONCAT’. Since we are concatenating the given vector ‘K’ times so, ‘CONCAT[i] = ARR[i%N]’. We use this fact to solve the problem without using additional space.
The algorithm will be -
Longest Subarray With Zero Sum
Merge Two Sorted Arrays Without Extra Space
Ninja And The Strictly Increasing Array
Negative To The End