New update is available. Click here to update.

Last Updated: 16 Nov, 2020

Difficulty: Moderate

```
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, ‘

The algorithm will be -

- We will loop from 0 to ‘
*N * K’*(loop variable*‘i’)*. - We initialize ‘
*CUR_SUM’*(to store sum of prefix elements) to 0 and ‘*MAX_SUM’*(to store maximum subarray sum) to the minimum possible value. - For each iteration, we will-
- Add ‘
*ARR[i % N]’*to ‘*CUR_SUM’’*. - Update ‘
*MAX_SUM’*if it’s value is less than ‘*CUR_SUM’’.* - If ‘
*CUR_SUM’’*becomes negative, we will reset it to 0.

- Add ‘
- Finally, we return ‘
*MAX_SUM’*as our answer.

Since ‘*CONCAT’ *is formed by concatenating ‘*ARR’ ‘K’ *times, we can break this problem into different cases to avoid redundant iterations and computations.

All the cases are-

- If
*K*= 1-- We simply apply Kadane’s algorithm and return the maximum sum.

- Else, we find the sum of elements (‘
*ARR_SUM'*). Now there are two cases-- If
*'ARR_SUM'*<= 0- Then, we will find the maximum subarray sum for ‘K’ = 2 irrespective of value of ‘
*K’*and return it as answer. - This is because the maximum subarray can’t contain complete ‘
*ARR’*. If we include the complete vector, then it will only decrease the subarray sum due to the negative total.

- Then, we will find the maximum subarray sum for ‘K’ = 2 irrespective of value of ‘
- If
*'ARR_SUM'*> 0- Then, we will find the maximum subarray sum for K = 2 and we return ‘
*MAX_SUBARRAY_SUM’*plus (*K*-2) times ‘*ARR_SUM'*as the answer. - This is because ‘
*ARR_SUM'*is greater than 0, including it ‘*K*-2’ times will only increase the subarray sum.

- Then, we will find the maximum subarray sum for K = 2 and we return ‘

- If

SIMILAR PROBLEMS

Missing Number

Posted: 30 Oct, 2022

Difficulty: Easy

Longest Subarray With Zero Sum

Posted: 3 Nov, 2022

Difficulty: Moderate

Merge Two Sorted Arrays Without Extra Space

Posted: 19 Nov, 2022

Difficulty: Moderate

Ninja And The Strictly Increasing Array

Posted: 27 Nov, 2022

Difficulty: Moderate

Negative To The End

Posted: 16 Dec, 2022

Difficulty: Easy

Popular Interview Problems: