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 an Array INPUT nums = [2, 3, 5, 10] 2 idx: 0 3 idx: 1 5 idx: 2 10 idx: 3 Problem Structure: n = 4 elements n-2 = 2 special numbers 1 sum element 1 outlier (to find) Total Sum: 2+3+5+10 = 20 ALGORITHM STEPS 1 Calculate Total Sum totalSum = 20 2 Store Count of Each Num HashMap for frequency 3 Try Each as Sum Check if outlier exists 4 Track Max Outlier Return largest valid Key Formula: outlier = totalSum - 2*sum sum=2: 20-4=16 (not in arr) sum=3: 20-6=14 (not in arr) sum=5: 20-10=10 (OK!) sum=10: 20-20=0 (not in arr) Special: 2,3 | Sum: 5 | Outlier: 10 FINAL RESULT Identified Components: Special Numbers 2 3 Sum Element 5 OUTLIER (Answer) 10 Verification: 2 + 3 = 5 (OK) Sum element exists (OK) 10 is largest outlier (OK) Key Insight: If we fix one element as the "sum" of special numbers, the outlier = totalSum - 2*sum. We iterate through each element as a potential sum, check if the calculated outlier exists in the array, and track the maximum valid outlier. Time: O(n), Space: O(n) using HashMap for element counts. TutorialsPoint - Identify the Largest Outlier in an Array | Optimal Solution
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