Last Updated: 16 Nov, 2020

Maximum subarray sum after K concatenation

Moderate
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'.

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

Approaches

01 Approach

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.

02 Approach

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-

  1. If K = 1-
    1. We simply apply Kadane’s algorithm and return the maximum sum.
  2. Else, we find the sum of elements (‘ARR_SUM'). Now there are two cases-
    1. If 'ARR_SUM' <= 0
      1. Then, we will find the maximum subarray sum for ‘K’ = 2 irrespective of value of ‘K’ and return it as answer.
      2. 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.
    2. If 'ARR_SUM' > 0
      1. 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.
      2. This is because ‘ARR_SUM' is greater than 0, including it ‘K-2’ times will only increase the subarray sum.