We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
public static void miniMaxSum(List<Integer> arr) {
// Write your code here
long sum = 0; // Using long to avoid overflow issues
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
// Calculate the total sum and find min and max values
for (int num : arr) {
sum += num;
min = Math.min(min, num);
max = Math.max(max, num);
}
// Calculate the minimum and maximum sums
long minSum = sum - max; // Sum of all except max
long maxSum = sum - min; // Sum of all except min
System.out.println(minSum + " " + maxSum);
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Mini-Max Sum
You are viewing a single comment's thread. Return to all comments →
SIMPLE JAVA8 CODING SOLUTION