Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
Given an integer array arr and an integer k, modify the array by repeating it k times.
For example, if arr = [1, 2] and k = 3 then the modified array will be [1, 2, 1, 2, 1, 2].
Return 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 can be very large, return the answer modulo 109 + 7.
Example 1:
Input: arr = [1,2], k = 3 Output: 9
Example 2:
Input: arr = [1,-2,1], k = 5 Output: 2
Example 3:
Input: arr = [-1,-2], k = 7 Output: 0
Constraints:
1 <= arr.length <= 1051 <= k <= 105-104 <= arr[i] <= 104Problem summary: Given an integer array arr and an integer k, modify the array by repeating it k times. For example, if arr = [1, 2] and k = 3 then the modified array will be [1, 2, 1, 2, 1, 2]. Return 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 can be very large, return the answer modulo 109 + 7.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[1,2] 3
[1,-2,1] 5
[-1,-2] 7
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1191: K-Concatenation Maximum Sum
class Solution {
public int kConcatenationMaxSum(int[] arr, int k) {
long s = 0, mxPre = 0, miPre = 0, mxSub = 0;
for (int x : arr) {
s += x;
mxPre = Math.max(mxPre, s);
miPre = Math.min(miPre, s);
mxSub = Math.max(mxSub, s - miPre);
}
long ans = mxSub;
final int mod = (int) 1e9 + 7;
if (k == 1) {
return (int) (ans % mod);
}
long mxSuf = s - miPre;
ans = Math.max(ans, mxPre + mxSuf);
if (s > 0) {
ans = Math.max(ans, (k - 2) * s + mxPre + mxSuf);
}
return (int) (ans % mod);
}
}
// Accepted solution for LeetCode #1191: K-Concatenation Maximum Sum
func kConcatenationMaxSum(arr []int, k int) int {
var s, mxPre, miPre, mxSub int
for _, x := range arr {
s += x
mxPre = max(mxPre, s)
miPre = min(miPre, s)
mxSub = max(mxSub, s-miPre)
}
const mod = 1e9 + 7
ans := mxSub
if k == 1 {
return ans % mod
}
mxSuf := s - miPre
ans = max(ans, mxSuf+mxPre)
if s > 0 {
ans = max(ans, mxSuf+(k-2)*s+mxPre)
}
return ans % mod
}
# Accepted solution for LeetCode #1191: K-Concatenation Maximum Sum
class Solution:
def kConcatenationMaxSum(self, arr: List[int], k: int) -> int:
s = mx_pre = mi_pre = mx_sub = 0
for x in arr:
s += x
mx_pre = max(mx_pre, s)
mi_pre = min(mi_pre, s)
mx_sub = max(mx_sub, s - mi_pre)
ans = mx_sub
mod = 10**9 + 7
if k == 1:
return ans % mod
mx_suf = s - mi_pre
ans = max(ans, mx_pre + mx_suf)
if s > 0:
ans = max(ans, (k - 2) * s + mx_pre + mx_suf)
return ans % mod
// Accepted solution for LeetCode #1191: K-Concatenation Maximum Sum
struct Solution;
const MOD: i32 = 1_000_000_007;
impl Solution {
fn k_concatenation_max_sum(arr: Vec<i32>, k: i32) -> i32 {
let sum: i32 = arr.iter().sum();
let mut prev = 0;
let mut res = 0;
let mut k = k as usize;
let n = arr.len();
for i in 0..n * k.min(2) {
prev = arr[i % n].max(prev + arr[i % n]);
res = res.max(prev);
}
while sum > 0 && k > 2 {
res += sum;
res %= MOD;
k -= 1
}
res
}
}
#[test]
fn test() {
let arr = vec![1, 2];
let k = 3;
let res = 9;
assert_eq!(Solution::k_concatenation_max_sum(arr, k), res);
let arr = vec![1, -2, 1];
let k = 5;
let res = 2;
assert_eq!(Solution::k_concatenation_max_sum(arr, k), res);
let arr = vec![-1, -2];
let k = 7;
let res = 0;
assert_eq!(Solution::k_concatenation_max_sum(arr, k), res);
}
// Accepted solution for LeetCode #1191: K-Concatenation Maximum Sum
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #1191: K-Concatenation Maximum Sum
// class Solution {
// public int kConcatenationMaxSum(int[] arr, int k) {
// long s = 0, mxPre = 0, miPre = 0, mxSub = 0;
// for (int x : arr) {
// s += x;
// mxPre = Math.max(mxPre, s);
// miPre = Math.min(miPre, s);
// mxSub = Math.max(mxSub, s - miPre);
// }
// long ans = mxSub;
// final int mod = (int) 1e9 + 7;
// if (k == 1) {
// return (int) (ans % mod);
// }
// long mxSuf = s - miPre;
// ans = Math.max(ans, mxPre + mxSuf);
// if (s > 0) {
// ans = Math.max(ans, (k - 2) * s + mxPre + mxSuf);
// }
// return (int) (ans % mod);
// }
// }
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.