LeetCode 3572, Medium. Topics: Array, Hash Table, Greedy, Sorting, Heap (Priority Queue). View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 22 parametrized pytest cases, and a playground notebook:
Problem
You are given two integer arrays <code>x</code> and <code>y</code>, each of length <code>n</code>. You must choose three <strong>distinct</strong> indices <code>i</code>, <code>j</code>, and <code>k</code> such that:
<ul>
<li><code>x[i] != x[j]</code></li>
<li><code>x[j] != x[k]</code></li>
<li><code>x[k] != x[i]</code></li>
</ul>
Your goal is to <strong>maximize</strong> the value of <code>y[i] + y[j] + y[k]</code> under these conditions. Return the <strong>maximum</strong> possible sum that can be obtained by choosing such a triplet of indices.
If no such triplet exists, return -1.
Examples
Constraints
- n == x.length == y.length
- 3 <= n <= 10^5
- 1 <= x[i], y[i] <= 10^6
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
NeetCode All. Last modified on September 7, 2026