Untitled
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; }
Leave a Comment