Maximum subarray sum after K concatenation

Moderate
0/80
Average time to solve is 15m
255 upvotes
Asked in company
Adobe

Problem statement

You have been given a vector/list 'ARR' consisting of ‘N’ integers. You are also given a positive integer ‘K’.

Let’s define a vector/list 'CONCAT' of size 'N * K' formed by concatenating 'ARR' ‘K’ times. For example, if 'ARR' = [0, -1, 2] and 'K' = 3, then 'CONCAT' is given by [0, -1, 2, 0, -1, 2, 0, -1, 2].

Your task is to find the maximum possible sum of any non-empty subarray (contagious) of 'CONCAT'.

Detailed explanation ( Input/output format, Notes, Images )
Input Format:
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.
Output Format :
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.
Note :
You do not need to print anything; it has already been taken care of. Just implement the function.
Constraints:
1 <= T <= 100
1 <= N <= 10^4
1 <= K <= 10^4    
-10^5 <= ARR[i] <= 10^5

Time Limit: 1sec
Sample Input 1 :
2
2 3
1 3
3 2
1 -2 1
Sample Output 1 :
12
2
Sample Output 1 Explanation:
For the first test case, vector 'CONCAT' is obtained by concatenating vector [1, 3] three times. 
'CONCAT' = [1, 3, 1, 3, 1, 3]

The subarray with a maximum sum of 12 is [1, 3, 1, 3, 1, 3].


For the second test case, vector 'CONCAT' is obtained by concatenating vector [1, -2, 1] two times. 
'CONCAT' = [1, -2, 1, 1, -2, 1]

The subarray with a maximum sum of 2 is [1, 1].
Sample Input 2 :
1
2 3
-2 1 
Sample Output 2 :
1
Hint

If a subarray has a prefix with negative sum, then should we keep this prefix or remove it from our subarray? 

Approaches (2)
Kadane's Algorithm

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 -

  1. We will loop from 0 to ‘N * K’ (loop variable ‘i’).
  2. 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.
  3. For each iteration, we will-
    1. Add ‘ARR[i % N]’ to ‘CUR_SUM’’.
    2. Update ‘MAX_SUM’ if it’s value is less than ‘CUR_SUM’’.
    3. If ‘CUR_SUM’’ becomes negative, we will reset it to 0.
  4. Finally, we return ‘MAX_SUM’ as our answer.
Time Complexity

O(N * K), where ‘N’ denotes the size of vector/list and ‘K’ is the given integer.

 

We are running a loop from 0 to ‘N * K’. So, the overall time complexity is O(N*K).

Space Complexity

O(1).

 

Constant space is used.

Video Solution
Unlock at level 3
(75% EXP penalty)
Code Solution
(100% EXP penalty)
Maximum subarray sum after K concatenation
Full screen
Console