You are given an integer array nums. This array contains n elements, where exactlyn - 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.
Since exactly n-2 elements are special numbers, and the other two are the sum and the outlier, we can try every possible pair of numbers as (sum, outlier), check if the rest can sum up to the sum, and return the largest valid outlier.
classSolution {
public:int findLargestOutlier(vector<int>& nums) {
int n = nums.size();
int ans = INT_MIN;
for (int i =0; i < n; ++i) {
for (int j =0; j < n; ++j) if (i != j) {
int sum =0;
for (int k =0; k < n; ++k) if (k != i && k != j) sum += nums[k];
if (sum == nums[i]) ans = max(ans, nums[j]);
}
}
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
funcfindLargestOutlier(nums []int) int {
ans:=-1<<31n:= len(nums)
fori:=0; i < n; i++ {
forj:=0; j < n; j++ {
ifi==j { continue }
sum:=0fork:=0; k < n; k++ {
ifk!=i&&k!=j {
sum+=nums[k]
}
}
ifsum==nums[i] &&nums[j] > ans {
ans = nums[j]
}
}
}
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution {
publicintfindLargestOutlier(int[] nums) {
int n = nums.length, ans = Integer.MIN_VALUE;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) if (i != j) {
int sum = 0;
for (int k = 0; k < n; ++k) if (k != i && k != j) sum += nums[k];
if (sum == nums[i]) ans = Math.max(ans, nums[j]);
}
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
funfindLargestOutlier(nums: IntArray): Int {
var ans = Int.MIN_VALUE
val n = nums.size
for (i in0 until n) {
for (j in0 until n) if (i != j) {
var sum = 0for (k in0 until n) if (k != i && k != j) sum += nums[k]
if (sum == nums[i]) ans = maxOf(ans, nums[j])
}
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution:
deffindLargestOutlier(self, nums: list[int]) -> int:
n = len(nums)
ans = float('-inf')
for i in range(n):
for j in range(n):
if i == j:
continue s = sum(nums[k] for k in range(n) if k != i and k != j)
if s == nums[i]:
ans = max(ans, nums[j])
return ans
impl Solution {
pubfnfind_largest_outlier(nums: Vec<i32>) -> i32 {
let n = nums.len();
letmut ans =i32::MIN;
for i in0..n {
for j in0..n {
if i == j { continue; }
letmut sum =0;
for k in0..n {
if k != i && k != j {
sum += nums[k];
}
}
if sum == nums[i] && nums[j] > ans {
ans = nums[j];
}
}
}
ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
findLargestOutlier(nums: number[]):number {
letans=-Infinity;
constn=nums.length;
for (leti=0; i<n; ++i) {
for (letj=0; j<n; ++j) if (i!==j) {
lets=0;
for (letk=0; k<n; ++k) if (k!==i&&k!==j) s+=nums[k];
if (s===nums[i]) ans= Math.max(ans, nums[j]);
}
}
returnans;
}
}
The sum of all special numbers is present in the array, and the outlier is the remaining number. For each number, try removing it and see if the rest can be split into (n-2) numbers and their sum.
For each number x in nums, let s = total - x. If s - y (for some y ≠ x) is also in nums, then x or y could be the outlier. Try all possibilities and track the largest valid outlier.
import java.util.*;
classSolution {
publicintfindLargestOutlier(int[] nums) {
Map<Integer, Integer> count =new HashMap<>();
long total = 0;
for (int x : nums) {
count.put(x, count.getOrDefault(x, 0) + 1);
total += x;
}
int res = Integer.MIN_VALUE;
for (int i = 0; i < nums.length; ++i) {
int x = nums[i];
long s = total - x;
for (int j = 0; j < nums.length; ++j) {
if (i == j) continue;
int y = nums[j];
if (s - y == y) {
int cnt = count.get(y);
if ((x == y && cnt >= 2) || (x != y && cnt >= 1)) {
res = Math.max(res, x);
}
}
}
}
return res;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from collections import Counter
deffindLargestOutlier(nums):
total = sum(nums)
count = Counter(nums)
res = float('-inf')
n = len(nums)
for i, x in enumerate(nums):
s = total - x
for j, y in enumerate(nums):
if i == j:
continueif s - y == y:
cnt = count[y]
if (x == y and cnt >=2) or (x != y and cnt >=1):
res = max(res, x)
return int(res)