K-Concatenation Maximum Sum in C++


Suppose we have an integer array arr and one integer k, we have to change the array by repeating it k times. So if arr = [1, 2] and k = 3 then the modified array will be [1, 2, 1, 2, 1, 2].

Now we have to find the maximum sub-array sum in the modified array. Note that the length of the sub-array can be 0 and its sum in that case is 0. As the answer may be very large, find the answer modulo 10^9 + 7.

So if the input is like [1,-2,1] and k = 5, then the result will be 2.

To solve this, we will follow these steps −

  • Define method called getKadane(), this will take array, this will work like −

  • ret := -inf, sum := 0, all values of ret and sum will be of mod 10^9 + 7

  • for i in range 0 to size of arr – 1

    • sum := max of arr[i] and arr[i] + sum

    • ret := max of ret, sum

  • if ret is < 0, then return 0, otherwise ret

  • Define method called getSum(), this will take array, this will work like −

  • ret := 0, The ret value will be of mod 10^9 + 7

  • for i in range 0 to size of arr – 1

    • ret := ret + arr[i]

  • return ret

  • Define method called getPrefix(), this will take array, this will work like −

  • ret := -inf, sum := 0, all values of ret and sum will be of mod 10^9 + 7

  • for i in range 0 to size of arr – 1

    • sum := sum + arr[i]

    • ret := max of ret and sum

  • if ret is < 0, then return 0, otherwise ret

  • Define method called getSuffix(), this will take array, this will work like −

  • ret := inf, sum := 0, all values of ret and sum will be of mod 10^9 + 7

  • for i in range size of arr – 1 down to 0

    • sum := sum + arr[i]

    • ret := max of ret and sum

  • if ret is < 0, then return 0, otherwise ret

  • From the main method, do the following −

  • kadane := getKadane(arr), sum := getSum(arr), prefix := getPrefix(arr), suffix := getSuffix(arr)

  • if k is 1, then return kadane

  • if sum > 1, then return max of (sum * (k - 2)) + prefix + suffix and kadane

  • otherwise return max of (prefix + suffix) and kadane

Example(C++)

Let us see the following implementation to get a better understanding −

 Live Demo

#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
const int MOD = 1e9 + 7;
int add(lli a, lli b){
   return ((a % MOD) + (b % MOD)) % MOD;
}
int mul(lli a, lli b){
   return ((a % MOD) * (b % MOD)) % MOD;
}
class Solution {
   public:
   int getKadane(vector <int>& arr){
      int ret = INT_MIN;
      int sum = 0;
      for(int i = 0; i < arr.size(); i++){
         sum = max(arr[i], arr[i] + sum);
         ret = max(ret, sum);
         sum %= MOD;
         ret %= MOD;
      }
      return ret < 0? 0 : ret;
   }
   int getSum(vector <int>& arr){
      int ret = 0;
      for(int i = 0; i < arr.size(); i++){
         ret += arr[i];
         ret %= MOD;
      }
      return ret;
   }
   int getPrefix(vector <int>& arr){
      int ret = INT_MIN;
      int sum = 0;
      for(int i = 0; i <arr.size(); i++){
         sum += arr[i];
         sum %= MOD;
         ret = max(ret, sum);
         ret %= MOD;
      }
      return ret < 0 ? 0 : ret;
   }
   int getSuffix(vector <int>& arr){
      int sum = 0;
      int ret = INT_MIN;
      for(int i = arr.size() - 1; i >= 0 ; i--){
         sum += arr[i];
         ret = max(ret, sum);
         sum %= MOD;
         ret %= MOD;
      }
      return ret < 0 ? 0 : ret;
   }
   int kConcatenationMaxSum(vector<int>& arr, int k) {
      int kadane = getKadane(arr);
      int sum = getSum(arr);
      int prefix = getPrefix(arr);
      int suffix = getSuffix(arr);
      if(k == 1) return kadane;
      if(sum > 0){
         return max((int)mul((k-2) , sum) + prefix % MOD + suffix % MOD, kadane);
      } else {
         return max(add(prefix , suffix), kadane);
      }
   }
};
main(){
   vector<int> v1 = {1,-2,1};
   Solution ob;
   cout << (ob.kConcatenationMaxSum(v1, 5));
}

Input

[1,-2,1]
5

Output

2

Updated on: 02-May-2020

235 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements