BackmediumArrays

Max Shifted Pair Score Solution

Problem Statement

Given an integer array `arr`, find the maximum score of a pair of indices `(i, j)` such that `0 <= i < j < arr.length`. The score of a pair is defined as: `score(i, j) = arr[i] + arr[j] + 2 * i - 3 * j` Return the maximum score among all valid pairs.

Example 1
Input
arr = [8, 1, 5, 2, 6]
Output
7

Explanation: For i = 0 and j = 2, the score is arr[0] + arr[2] + 2 * 0 - 3 * 2 = 8 + 5 + 0 - 6 = 7. No other pair has a higher score.

Example 2
Input
arr = [1, 10, 2]
Output
8

Explanation: For i = 0 and j = 1, the score is arr[0] + arr[1] + 2 * 0 - 3 * 1 = 1 + 10 + 0 - 3 = 8. Another pair (i = 1, j = 2) also gives a score of 10 + 2 + 2 - 6 = 8.

Constraints

  • 2 <= arr.length <= 10^5
  • 1 <= arr[i] <= 10^4
Live Compiler
Sign InSign Up
Loading...
Test Cases & Output
Click "Run" to execute