Mini-Max Sum

Sort by

recency

|

370 Discussions

|

  • + 0 comments

    log(n) in Python:

    def miniMaxSum(arr):
        # Write your code here
        
    
       max_element = max(arr)
       min_element = min(arr)
       total = sum(arr)
       print(total-max_element,total-min_element)
    
  • + 0 comments

    Shortest solution in Python: def miniMaxSum(arr): # Write your code here

    arr.sort()
    print(sum(arr[:-1]),sum(arr[1:]))
    
  • + 0 comments

    Simple Javascript solution here, works by sorting the initial array and then reducing the first 4 indices, then last 4 indices to return the min and max sum

    function miniMaxSum(arr) { // Write your code here let sorted = [...arr.sort((a, b)=>{return a-b})]; console.log(sorted.slice(0, 4).reduce((a, b)=>{ return a+b }, 0), sorted.slice(1).reduce((a, b)=>{ return a+b }, 0)) }

  • + 0 comments

    Java:

    long summ=0,suml=0; Collections.sort(arr); for(int i=0;i<4;i++){ summ+=arr.get(i); } for(int i=arr.size()-1;i>arr.size()-5;i--){ suml+=arr.get(i); } System.out.println(summ+" "+suml); }

  • + 0 comments

    c++ short solution using only functions from "algorithm" library

    #define all(a) (a).begin(),(a).end()
    #define ll long long
    
    void miniMaxSum(vector<int> arr) {
        ll sum = accumulate(all(arr),0ll);
        int mn = *min_element(all(arr));
        int mx = *max_element(all(arr));
        cout << sum-mx << " " << sum-mn << '\n';
    }```