DSAMaster Logo
DSAMaster
BackmediumArrays

Maximum Position-Adjusted Gain

Problem Description

You are given an integer array nums. Find the maximum value of the expression:

nums[j] - nums[i] - (j - i)

over all pairs of indices (i, j) such that 0 <= i < j < nums.length.

Example 1
Input
nums = [10, 11, 14, 12, 18]
Output
5

Explanation: Choosing i = 3 and j = 4 yields nums[4] - nums[3] - (4 - 3) = 18 - 12 - 1 = 5. No other pair produces a larger result.

Example 2
Input
nums = [1, 5, 2, 8]
Output
5

Explanation: Choosing i = 2 and j = 3 yields nums[3] - nums[2] - (3 - 2) = 8 - 2 - 1 = 5.

Example 3
Input
nums = [5, 4, 3, 2, 1]
Output
-2

Explanation: Choosing i = 0 and j = 1 yields nums[1] - nums[0] - (1 - 0) = 4 - 5 - 1 = -2.

Constraints

  • 2 <= nums.length <= 10^5
  • -10^4 <= nums[i] <= 10^4
00:00
Loading...

Sign in to Solve

Access the full code editor, ThinkBuddy AI hints, and track your progress.

Sign in Free
Test Cases & Output
Enter test input here...
Click "Run Code" to execute