You are viewing a single comment's thread. Return to all comments →
Java 8 based
public static void miniMaxSum(List<Integer> arr) { // Optimized implementation // Time Complexity O(n) (linear time) // Space Complexity O(1) (constant space) long totalSum = 0; long minValue= Long.MAX_VALUE; long maxValue = Long.MIN_VALUE; for(Integer i : arr) { totalSum += i; } for(Integer i : arr) { long currentSum = totalSum - i; if(currentSum < minValue) minValue = currentSum; if(currentSum > maxValue) maxValue = currentSum; } System.out.println(minValue + " " + maxValue); // Non Optimized implementation // Time Complexity O(n *2) (quadratic time) // Space Complexity O(n) (linear space) List<Long> sumList = new ArrayList<>(); int tracker = 0; int arraySize = arr.size(); while(tracker != arraySize) { long sum = 0; for(int i = 0; i < arraySize; i++) { if(tracker == i) { continue; } sum += arr.get(i); } tracker++; sumList.add(sum); sum = 0; } List<Long> sortedIntegers = sumList.stream() .sorted() .collect(Collectors.toList()); System.out.println(sortedIntegers.get(0) + " " + sortedIntegers.get(sortedIntegers.size() - 1)); }
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 →
Java 8 based