Identify the Largest Outlier in an Array - Problem

You are given an integer array nums. This array contains n elements, where exactly n - 2 elements are special numbers.

One of the remaining two elements is the sum of these special numbers, and the other is an outlier.

An outlier is defined as a number that is neither one of the original special numbers nor the element representing the sum of those numbers.

Note that special numbers, the sum element, and the outlier must have distinct indices, but may share the same value.

Return the largest potential outlier in nums.

Input & Output

Example 1 — Basic Case
$ Input: nums = [2,3,5,10]
Output: 10
💡 Note: Array has n=4 elements, so n-2=2 special numbers. If outlier=10, remaining=[2,3,5]. Sum of specials should equal sum element: 2+3=5, and 5 exists. So 10 is valid outlier.
Example 2 — Multiple Valid Outliers
$ Input: nums = [-2,-1,-3,-6,4]
Output: 4
💡 Note: If outlier=4, remaining=[-2,-1,-3,-6]. Sum of [-2,-1,-3]=-6, and -6 exists. If outlier=-1, remaining=[-2,-3,-6,4]. Sum of [-2,-3]=-5, but -5 doesn't exist. So 4 is the largest valid outlier.
Example 3 — Edge Case with Duplicates
$ Input: nums = [1,1,1]
Output: 1
💡 Note: With n=3, need n-2=1 special number. If outlier=1 (index 0), remaining=[1,1]. Sum of 1 special = 1, sum element = 1. Valid configuration exists.

Constraints

  • 3 ≤ nums.length ≤ 105
  • -1000 ≤ nums[i] ≤ 1000

Visualization

Tap to expand
Identify the Largest Outlier in Array23510SpecialSpecialSum=2+3Outliern=4 elements: 2 specials + 1 sum + 1 outlierTest: If 10 is outlier, remaining=[2,3,5]✓ Sum of specials [2,3] = 5, which exists in arrayOutput: 10 (largest valid outlier)
Understanding the Visualization
1
Input Analysis
Array with n-2 specials, 1 sum, 1 outlier
2
Test Each Element
Check if element can be outlier by validating remaining
3
Find Maximum
Return largest valid outlier
Key Takeaway
🎯 Key Insight: For each potential outlier, remaining elements must perfectly split into specials and their sum
Asked in
Amazon 25 Microsoft 20
35.6K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen