[LeetCode] Identify the Largest Outlier in an Array

3371. Identify the Largest Outlier in an Array

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
int getLargestOutlier(vector<int>& nums) {
int sum = accumulate(begin(nums), end(nums), 0);
unordered_map<int,int> f;
int res = INT_MIN;
for(auto& n : nums) f[n]++;
for(auto& [k1,v1] : f) {
for(auto& [k2,v2] : f) {
if(k1 == k2 and v1 == 1) continue;
int s = sum - k1 - k2;
if(k1 == s) {
res = max(res, k2);
}
if(k2 == s) {
res = max(res, k1);
}
}
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/12/01/PS/LeetCode/identify-the-largest-outlier-in-an-array/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.