Maximize the sum of two array values minus the distance between their indices

Quick Overview

Given an array of sightseeing spot values, find the pair of indices i < j that maximizes the sum of their two values minus the distance between them. The problem tests whether you can rearrange a pairwise score so that a quadratic search over all pairs becomes a single linear scan.

Maximize the sum of two array values minus the distance between their indices

Company: Wayfair

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given an integer array `values` where `values[i]` is the value of the sightseeing spot at index `i`. Two sightseeing spots `i` and `j` have a distance of `j - i` between them. The score of a pair of sightseeing spots with `i < j` is `values[i] + values[j] + i - j`: the sum of the two values, minus the distance between them. Return the maximum score of a pair of sightseeing spots. ### Function Signature ```python def max_score_sightseeing_pair(values: list[int]) -> int: ``` ### Constraints - `2 <= len(values) <= 5 * 10^4` - `1 <= values[i] <= 1000` ### Examples ```text Input: values = [8, 1, 5, 2, 6] Output: 11 ``` Choosing `i = 0` and `j = 2` gives `8 + 5 + 0 - 2 = 11`. The next best pair is `(0, 4)`, which scores `8 + 6 + 0 - 4 = 10`. ```text Input: values = [1, 2] Output: 2 ``` The only pair is `(0, 1)`, which scores `1 + 2 + 0 - 1 = 2`.

Overview: Given an array of sightseeing spot values, find the pair of indices i < j that maximizes the sum of their two values minus the distance between them. The problem tests whether you can rearrange a pairwise score so that a quadratic search over all pairs becomes a single linear scan.

Wayfair logo
Wayfair
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given an integer array values where values[i] is the value of the sightseeing spot at index i. Two sightseeing spots i and j have a distance of j - i between them.

The score of a pair of sightseeing spots with i < j is values[i] + values[j] + i - j: the sum of the two values, minus the distance between them.

Return the maximum score of a pair of sightseeing spots.

Function Signature

def max_score_sightseeing_pair(values: list[int]) -> int:

Constraints

  • 2 <= len(values) <= 5 * 10^4
  • 1 <= values[i] <= 1000

Examples

Input: values = [8, 1, 5, 2, 6]
Output: 11

Choosing i = 0 and j = 2 gives 8 + 5 + 0 - 2 = 11. The next best pair is (0, 4), which scores 8 + 6 + 0 - 4 = 10.

Input: values = [1, 2]
Output: 2

The only pair is (0, 1), which scores 1 + 2 + 0 - 1 = 2.