Skip to main content
Back to problems
Leetcode
Medium
Arrays
Dynamic Programming
Google
Meta
K Concatenation Maximum Sum

Find the maximum subarray sum obtainable from an array repeated kk times, with the answer taken modulo 109+710^9+7 when needed.

Acceptance 0%
Problem Statement

Problem

Given an integer array arr and an integer k, form a new array by concatenating arr to itself exactly k times.

Return the maximum possible sum of a non-empty contiguous subarray of this concatenated array.

Because the value can be large, return the result modulo 109+710^9 + 7.

Notes

  • The subarray must be contiguous.
  • The subarray must be non-empty.
  • If all values are negative, the best answer is the largest single element.

Intuition

The answer depends on whether the best subarray lies:

  1. entirely inside one copy of arr,
  2. across the boundary between two copies, or
  3. across many copies when the total sum of arr is positive.

Input Format

  • An integer array arr
  • An integer k

Output Format

  • Return one integer: the maximum subarray sum in arr repeated k times, modulo 109+710^9 + 7.

Constraints

  • 1 <= arr.length
  • 1 <= k
  • Elements of arr may be negative, zero, or positive
  • The subarray must be non-empty
Examples
Sample cases returned by the problem API.

Example 1

Input

arr = [1, 2], k = 3

Output

9

Explanation

The concatenated array is [1, 2, 1, 2, 1, 2]. The whole array has sum 9, which is the maximum subarray sum.

Example 2

Input

arr = [1, -2, 1], k = 5

Output

2

Explanation

The best subarray is [1, -2, 1, 1, -2, 1] or any equivalent contiguous segment with sum 2 across the boundary between copies.

Show 1 more example

Example 3

Input

arr = [-1, -2], k = 4

Output

0

Explanation

The best non-empty subarray sum is -1, and after applying modulo 109+710^9 + 7 the result is 1000000006. However, in many interview-style formulations the answer is reported as max(0, bestSum); this example is illustrative only.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.