Maximum Total Subarray Value I
Description:
Given an integer array nums and an integer k, you are allowed to choose a subarray exactly k times. The value of a subarray is defined as the maximum element minus the minimum element within that subarray.
Return the maximum total value you can achieve.
Example 1:
Input: nums = [1, 2, 3, 4], k = 2
Output: 6
Explanation:
The optimal subarray is the entire array [1, 2, 3, 4]. Its value is 4 - 1 = 3. We choose this subarray k = 2 times, yielding a total value of 3 * 2 = 6.
Video Explanation

Approaches:
1. Greedy Algorithm
Since we can choose the exact same subarray multiple times, the optimal strategy is simply to find the single subarray that yields the absolute maximum value and choose it k times. The maximum possible value for any subarray is just the global maximum element minus the global minimum element of the entire array.
- Traverse the array to find the global maximum and global minimum elements.
- The maximum possible value of any valid subarray will be the difference between these two:
global_max - global_min. - Multiply this maximum difference by
kto get the optimal total value.
Note on Integer Overflow: The array values can be up to and
kup to . This means the maximum possible result is , which exceeds the limit of a standard 32-bit signed integer. We must use 64-bit integers (long longin C++,longin Java) to prevent overflow.
- Time Complexity: where is the length of the
numsarray. We only need a single pass through the array to find the minimum and maximum values. - Space Complexity: because we are only using a few extra variables for the bounds, taking constant extra space.
Solutions
- C++
- Java
- Python
- JavaScript
class Solution {
public:
long long maxTotalValue(vector<int>& nums, int k) {
int min_val = nums[0];
int max_val = nums[0];
for (int num : nums) {
if (num < min_val) min_val = num;
if (num > max_val) max_val = num;
}
return ((long long)max_val - min_val) * k;
}
};
class Solution {
public long maxTotalValue(int[] nums, int k) {
int min_val = nums[0];
int max_val = nums[0];
for (int num : nums) {
if (num < min_val) min_val = num;
if (num > max_val) max_val = num;
}
return ((long)max_val - min_val) * k;
}
}
class Solution:
def maxTotalValue(self, nums: List[int], k: int) -> int:
# Python automatically handles arbitrarily large integers
return (max(nums) - min(nums)) * k
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var maxTotalValue = function(nums, k) {
let min_val = nums[0];
let max_val = nums[0];
for (let num of nums) {
if (num < min_val) min_val = num;
if (num > max_val) max_val = num;
}
return (max_val - min_val) * k;
};
Done with this topic? Mark it as complete to track your progress.
Related Practice Problems
Handpicked problems sharing similar algorithmic topic tags
Crimson Triples
Solution for Codeforces 2238B: Crimson Triples, utilizing mathematical observations and a greedy approach.
Destroying Asteroids
Finding if a planet can destroy all asteroids using a greedy approach.
Earliest Finish Time for Land and Water Rides I/II
Finding the earliest possible completion time for taking exactly one land ride and one water ride using a greedy approach.
💬 Discuss this page
Have a question or spot something confusing in "Maximum Total Subarray Value I"? Ask below — it's backed by GitHub Discussions, so maintainers get notified like any other GitHub activity.