Untitled
unknown
plain_text
a year ago
718 B
7
Indexable
public static int findGreatestOutlier(List<Integer> arr) {
// Calculate the total sum of the list elements
int totalSum = arr.stream().mapToInt(Integer::intValue).sum();
Set<Integer> originalNumbers = new HashSet<>(arr);
int greatestOutlier = Integer.MIN_VALUE;
for (int num : arr) {
// Compute the sum of the list excluding the current number
int sumExcludingNum = totalSum - num;
// Check if 'num' is a potential outlier
if (!originalNumbers.contains(sumExcludingNum) && sumExcludingNum != num) {
greatestOutlier = Math.max(greatestOutlier, num);
}
}
return greatestOutlier;
}Editor is loading...
Leave a Comment