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.
- Mini-Max Sum
- Discussions
Mini-Max Sum
Mini-Max Sum
+ 9 comments - If you use built in Python sort(), big O time complexity is already (n log n). If you then find sum(), it's (n) complexity. So it gives (n log n + n) and after simplifying (n log n).
- If you use sum(), max(), min() is's (n+n+n) = (3n) and after simplifying (n).
- Here is an example of one loop solution with time complexity (n) (but it does really matter only for an astronomical length of an input)
def miniMaxSum(arr): min_ = arr[0] max_ = arr[0] sum_ = 0 for i in arr: sum_ += i if i > max_: max_ = i if i < min_: min_ = i print(sum_ - max_, sum_ - min_)
+ 6 comments JavaScript is kinda cheating
function miniMaxSum(arr) { arr.sort((a, b) => a - b); let min = arr.slice(0, arr.length - 1).reduce((a,b) => a+b) let max = arr.slice(1).reduce((a,b) => a+b) console.log(min, max) }
+ 4 comments def miniMaxSum(arr): # Write your code here print(sum(arr)-max(arr),end=" ") print(sum(arr)-min(arr))
+ 1 comment JAVA CODE public static void miniMaxSum(List arr) { long sumMax = 0; long max = Integer.MIN_VALUE; long min = Integer.MAX_VALUE; long sumMin = 0;
for(Integer i: arr) { if(i <= min){ min = i; } if (i >= max){ max = i; } } for(Integer j: arr){ if(min != j){ sumMax += j; } if(max != j){ sumMin += j; } if(min == max){ sumMax = sumMin = (min * (arr.size()-1)); } } System.out.println(sumMin + " " + sumMax); }
+ 4 comments Python Code
def miniMaxSum(arr): arr.sort() print(sum(arr[:4]), sum(arr[1:])) arr=list(map(int ,input().rstrip().split())) miniMaxSum(arr)
Load more conversations
Sort 311 Discussions, By:
Please Login in order to post a comment