
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 −
#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
- Related Articles
- Maximum subarray sum in an array created after repeated concatenation in C++
- Maximum subarray sum in an array created after repeated concatenation in C++ Program
- Maximum Size Subarray Sum Equals k in C++
- Python – Sort Matrix by K Sized Subarray Maximum Sum
- Maximum sum subsequence with at-least k distant elements in C++
- Program to find sum of k non-overlapping sublists whose sum is maximum in C++
- Maximum sum subsequence with at-least k distant elements in C++ program
- Program to find maximum sum by removing K numbers from ends in python
- Maximum array sum that can be obtained after exactly k changes in C++
- Find maximum (or minimum) sum of a subarray of size k in C++
- Maximum subarray sum by flipping signs of at most K array elements in C++
- Program to find maximum sum by performing at most k negate operations in Python
- Maximum subarray sum in array formed by repeating the given array k times in C++
- Pick maximum sum M elements such that contiguous repetitions do not exceed K in C++
- Find maximum sum of triplets in an array such than i < j < k and a[i] < a[j] < a[k] in Python
