#3572

Maximize Y‑Sum by Picking a Triplet of Distinct X‑Values

specialist · 675 · lc medium +30 · 63.2% accepted · 69 likes · top 65%

Description

Given two integer arrays x and y of length n, choose three distinct indices i, j, and k such that all three corresponding x values are pairwise distinct:

- x[i] != x[j]

- x[j] != x[k]

- x[k] != x[i]

Maximize y[i] + y[j] + y[k] and return the largest achievable sum.

If no valid triplet exists, return -1.

Code

1
2
3