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.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
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] <= 105Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math · Greedy
[2,3,1,5,4]
[2,4,9,24,2,1,10]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1330: Reverse Subarray To Maximize Array Value
class Solution {
public int maxValueAfterReverse(int[] nums) {
int n = nums.length;
int s = 0;
for (int i = 0; i < n - 1; ++i) {
s += Math.abs(nums[i] - nums[i + 1]);
}
int ans = s;
for (int i = 0; i < n - 1; ++i) {
ans = Math.max(
ans, s + Math.abs(nums[0] - nums[i + 1]) - Math.abs(nums[i] - nums[i + 1]));
ans = Math.max(
ans, s + Math.abs(nums[n - 1] - nums[i]) - Math.abs(nums[i] - nums[i + 1]));
}
int[] dirs = {1, -1, -1, 1, 1};
final int inf = 1 << 30;
for (int k = 0; k < 4; ++k) {
int k1 = dirs[k], k2 = dirs[k + 1];
int mx = -inf, mi = inf;
for (int i = 0; i < n - 1; ++i) {
int a = k1 * nums[i] + k2 * nums[i + 1];
int b = Math.abs(nums[i] - nums[i + 1]);
mx = Math.max(mx, a - b);
mi = Math.min(mi, a + b);
}
ans = Math.max(ans, s + Math.max(0, mx - mi));
}
return ans;
}
}
// Accepted solution for LeetCode #1330: Reverse Subarray To Maximize Array Value
func maxValueAfterReverse(nums []int) int {
s, n := 0, len(nums)
for i, x := range nums[:n-1] {
y := nums[i+1]
s += abs(x - y)
}
ans := s
for i, x := range nums[:n-1] {
y := nums[i+1]
ans = max(ans, s+abs(nums[0]-y)-abs(x-y))
ans = max(ans, s+abs(nums[n-1]-x)-abs(x-y))
}
dirs := [5]int{1, -1, -1, 1, 1}
const inf = 1 << 30
for k := 0; k < 4; k++ {
k1, k2 := dirs[k], dirs[k+1]
mx, mi := -inf, inf
for i, x := range nums[:n-1] {
y := nums[i+1]
a := k1*x + k2*y
b := abs(x - y)
mx = max(mx, a-b)
mi = min(mi, a+b)
}
ans = max(ans, s+max(mx-mi, 0))
}
return ans
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
# Accepted solution for LeetCode #1330: Reverse Subarray To Maximize Array Value
class Solution:
def maxValueAfterReverse(self, nums: List[int]) -> int:
ans = s = sum(abs(x - y) for x, y in pairwise(nums))
for x, y in pairwise(nums):
ans = max(ans, s + abs(nums[0] - y) - abs(x - y))
ans = max(ans, s + abs(nums[-1] - x) - abs(x - y))
for k1, k2 in pairwise((1, -1, -1, 1, 1)):
mx, mi = -inf, inf
for x, y in pairwise(nums):
a = k1 * x + k2 * y
b = abs(x - y)
mx = max(mx, a - b)
mi = min(mi, a + b)
ans = max(ans, s + max(mx - mi, 0))
return ans
// Accepted solution for LeetCode #1330: Reverse Subarray To Maximize Array Value
struct Solution;
impl Solution {
fn max_value_after_reverse(nums: Vec<i32>) -> i32 {
let n = nums.len();
let mut res = 0;
let mut min = std::i32::MAX;
let mut max = std::i32::MIN;
let mut total = 0;
for i in 1..n {
let a = nums[i - 1];
let b = nums[i];
total += (a - b).abs();
res = res.max((b - nums[0]).abs() - (a - b).abs());
res = res.max((a - nums[n - 1]).abs() - (a - b).abs());
min = min.min(a.max(b));
max = max.max(a.min(b));
}
total + res.max((max - min) * 2)
}
}
#[test]
fn test() {
let nums = vec![2, 3, 1, 5, 4];
let res = 10;
assert_eq!(Solution::max_value_after_reverse(nums), res);
let nums = vec![2, 4, 9, 24, 2, 1, 10];
let res = 68;
assert_eq!(Solution::max_value_after_reverse(nums), res);
}
// Accepted solution for LeetCode #1330: Reverse Subarray To Maximize Array Value
function maxValueAfterReverse(nums: number[]): number {
const n = nums.length;
let s = 0;
for (let i = 0; i < n - 1; ++i) {
s += Math.abs(nums[i] - nums[i + 1]);
}
let ans = s;
for (let i = 0; i < n - 1; ++i) {
const d = Math.abs(nums[i] - nums[i + 1]);
ans = Math.max(ans, s + Math.abs(nums[0] - nums[i + 1]) - d);
ans = Math.max(ans, s + Math.abs(nums[n - 1] - nums[i]) - d);
}
const dirs = [1, -1, -1, 1, 1];
const inf = 1 << 30;
for (let k = 0; k < 4; ++k) {
let mx = -inf;
let mi = inf;
for (let i = 0; i < n - 1; ++i) {
const a = dirs[k] * nums[i] + dirs[k + 1] * nums[i + 1];
const b = Math.abs(nums[i] - nums[i + 1]);
mx = Math.max(mx, a - b);
mi = Math.min(mi, a + b);
}
ans = Math.max(ans, s + Math.max(0, mx - mi));
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
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: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.