FastPrepMaximize Adjacent Difference With One Reversal

Maximize Adjacent Difference With One Reversal

Amazon logoAmazonâ—Ź HardINTERNPHONE SCREEN

Problem statement

Given an integer array values, define its score as the sum of abs(values[i] - values[i + 1]) over every adjacent pair.

You may reverse at most one contiguous subarray. Return the maximum score obtainable after the optional reversal.

Function

maximizeArrayScore(values: int[]) → long

Examples

Example 1

values = [2,3,1,5,4]return = 10

The original score is 8. Reversing the subarray [3,1,5] produces [2,5,1,3,4], whose score is 10.

Example 2

values = [1,2,3,4]return = 5

Reversing [2,3] produces [1,3,2,4], with score 2 + 1 + 2 = 5.

Constraints

  • 1 <= values.length <= 10^5
  • -10^9 <= values[i] <= 10^9

More Amazon problems

See Amazon hiring insights
Saved
public long maximizeArrayScore(int[] values) {
  // write your code here
}
Checking account…