1330. Reverse Subarray To Maximize Array Value LeetCode Solution
In this guide, you will get 1330. Reverse Subarray To Maximize Array Value LeetCode Solution with the best time and space complexity. The solution to Reverse Subarray To Maximize Array Value problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Reverse Subarray To Maximize Array Value solution in C++
- Reverse Subarray To Maximize Array Value solution in Java
- Reverse Subarray To Maximize Array Value solution in Python
- Additional Resources

Problem Statement of Reverse Subarray To Maximize Array Value
You are given an integer array nums. The value of this array is defined as the sum of |nums[i] – nums[i + 1]| for all 0 <= i < nums.length – 1.
You are allowed to select any subarray of the given array and reverse it. You can perform this operation only once.
Find maximum possible value of the final array.
Example 1:
Input: nums = [2,3,1,5,4]
Output: 10
Explanation: By reversing the subarray [3,1,5] the array becomes [2,5,1,3,4] whose value is 10.
Example 2:
Input: nums = [2,4,9,24,2,1,10]
Output: 68
Constraints:
2 <= nums.length <= 3 * 104
-105 <= nums[i] <= 105
The answer is guaranteed to fit in a 32-bit integer.
Complexity Analysis
- Time Complexity:
- Space Complexity:
1330. Reverse Subarray To Maximize Array Value LeetCode Solution in C++
class Solution {
public:
int maxValueAfterReverse(vector<int>& nums) {
int total = 0;
int mn = INT_MAX;
int mx = INT_MIN;
for (int i = 0; i + 1 < nums.size(); ++i) {
const int a = nums[i];
const int b = nums[i + 1];
total += abs(a - b);
mn = min(mn, max(a, b));
mx = max(mx, min(a, b));
}
int diff = max(0, (mx - mn) * 2);
for (int i = 0; i + 1 < nums.size(); ++i) {
const int a = nums[i];
const int b = nums[i + 1];
const int headDiff = -abs(a - b) + abs(nums.front() - b);
const int tailDiff = -abs(a - b) + abs(nums.back() - a);
diff = max({diff, headDiff, tailDiff});
}
return total + diff;
}
};
/* code provided by PROGIEZ */
1330. Reverse Subarray To Maximize Array Value LeetCode Solution in Java
class Solution {
public int maxValueAfterReverse(int[] nums) {
int total = 0;
int mn = Integer.MAX_VALUE;
int mx = Integer.MIN_VALUE;
for (int i = 0; i + 1 < nums.length; ++i) {
final int a = nums[i];
final int b = nums[i + 1];
total += Math.abs(a - b);
mn = Math.min(mn, Math.max(a, b));
mx = Math.max(mx, Math.min(a, b));
}
int diff = Math.max(0, (mx - mn) * 2);
for (int i = 0; i + 1 < nums.length; ++i) {
final int a = nums[i];
final int b = nums[i + 1];
final int headDiff = -Math.abs(a - b) + Math.abs(nums[0] - b);
final int tailDiff = -Math.abs(a - b) + Math.abs(nums[nums.length - 1] - a);
diff = Math.max(diff, Math.max(headDiff, tailDiff));
}
return total + diff;
}
}
// code provided by PROGIEZ
1330. Reverse Subarray To Maximize Array Value LeetCode Solution in Python
class Solution:
def maxValueAfterReverse(self, nums: list[int]) -> int:
mn = math.inf
mx = -math.inf
for a, b in zip(nums, nums[1:]):
mn = min(mn, max(a, b))
mx = max(mx, min(a, b))
diff = max(0, (mx - mn) * 2)
for a, b in zip(nums, nums[1:]):
headDiff = -abs(a - b) + abs(nums[0] - b)
tailDiff = -abs(a - b) + abs(nums[-1] - a)
diff = max(diff, headDiff, tailDiff)
return sum(abs(a - b) for a, b in zip(nums, nums[1:])) + diff
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.