Maximum Width Ramp

Medium Topics Company Tags

Find two positions i < j such that nums[i] <= nums[j]. Return the largest possible distance j - i, or 0 if no pair satisfies the condition.

Example 1:

Input: nums = [4,1,2,3]

Output: 2

Explanation: Choose indices 1 and 3: the values satisfy 1 <= 3 and the width is 3 - 1 = 2. Index 0 cannot start a wider ramp because its value exceeds every later value.

Example 2:

Input: nums = [4,3,2,1]

Output: 0

Explanation: The values strictly decrease, so no earlier entry is less than or equal to a later entry. There is no ramp.

Example 3:

Input: nums = [2,2]

Output: 1

Explanation: The equal values at indices 0 and 1 satisfy the condition, giving width 1.

Constraints:

  • 2 <= nums.length <= 50000
  • 0 <= nums[i] <= 50000


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.