<p>You are given an integer array <code>nums</code>. The <em>value</em> of this array is defined as the sum of <code>|nums[i] - nums[i + 1]|</code> for all <code>0 &lt;= i &lt; nums.length - 1</code>.</p>

<p>You are allowed to select any subarray of the given array and reverse it. You can perform this operation <strong>only once</strong>.</p>

<p>Find maximum possible value of the final array.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,3,1,5,4]
<strong>Output:</strong> 10
<b>Explanation: </b>By reversing the subarray [3,1,5] the array becomes [2,5,1,3,4] whose value is 10.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,4,9,24,2,1,10]
<strong>Output:</strong> 68
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= nums.length &lt;= 3 * 10<sup>4</sup></code></li>
	<li><code>-10<sup>5</sup> &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
	<li>The answer is guaranteed to fit in a 32-bit integer.</li>
</ul>

class Solution:
    def maxValueAfterReverse(self, nums: List[int]) -> int:
        '''
        * value of arr is abs(arr[i+1]-arr[i]) for every element except the last one
        * choose a subarray and reverse it such that the resulting value of the modified array is maximum
        ========
        * at each i, I can choose to improve the left part by reversing the current pair or choose to improve the right part by improving the left part..
        * if I know two such extreme i's then I know reversing which will give me the max.. 
        '''

        initValue = sum(abs(prev - curr) for prev, curr in zip(nums, nums[1:]))
        overall_min_among_every_max, overall_max_among_every_min = -inf, inf

        maxx_boost = 0
        for prev, curr in zip(nums, nums[1:]): 
            curr_diff = abs(curr - prev)
            ### how will a reversal affect this pair? 
            left_part = abs(curr - nums[0])
            right_part = abs(nums[-1] - prev)

            maxx_boost = max(maxx_boost, max(left_part - curr_diff, right_part - curr_diff)) # maximum score boost I can get from this reversal; this way I know the max impact on the final array value this pair has.. 

            curr_pair_min, curr_pair_max = min(prev, curr), max(prev, curr) # for the current pair I am looking at.. which one is the minimum and which is the maximum? 

            overall_max_among_every_min = min(curr_pair_max, overall_max_among_every_min) # is the current pair maximum the overall min among the maximums.. 
            overall_min_among_every_max = max(curr_pair_min, overall_min_among_every_max) # is the current pair minimum the overall max among the minimums.. ie., the closest gap between the extremes.. 

        
        improved_score = initValue + max(maxx_boost, 2 * (overall_min_among_every_max - overall_max_among_every_min)) # the final pair or the best I found so far..
        return improved_score
21200