Sort by

recency

|

6014 Discussions

|

  • + 0 comments

    I think this is the best way:

    function miniMaxSum(arr) {
        const highest = Math.max(...arr);
        const lowest = Math.min(...arr);
        const sum = arr.reduce((a, b) => a + b, 0);
        console.log((sum - highest) + ' ' + (sum - lowest));
    }
    
  • + 0 comments

    Java Solution. Sum the whole array values and at the same time get the min and max values from the array. At the end just substract the min and max values from the sum.

        public static void miniMaxSum(List<Integer> arr) {
        // Write your code here
            int min = Integer.MAX_VALUE;
            int max = Integer.MIN_VALUE;
            long sum = 0l;
            
            for(Integer val : arr){
                sum += val;
                min = Math.min(min,val);
                max = Math.max(max,val);
            }
            
            System.out.print(sum-max);
            System.out.print(" ");
            System.out.print(sum-min);
    
        }
    
  • + 0 comments

    C#:

    //Way 01:
    public static void miniMaxSum(List<int> arr)
    {
        arr.Sort();
        int minSum = 0, maxSum = 0;
        
        for (int i = 0; i < arr.Count - 1; i++)
        {
            minSum += arr[i];
            maxSum += arr[i + 1];
        }
    
        Console.WriteLine($"{minSum} {maxSum}");
    		}
    
    //Way 02:
    
    public static void miniMaxSum(List<int> arr)
    {
        long _minSum = 0;
        long _maxSum = 0;
        int j = arr.Count - 1;
        arr.Sort();
        for (int i = 0; i < arr.Count; i++, j--)
        {
            if (i < arr.Count - 1)
            {
                _minSum += arr[i];
            }
            if (j != 0 && j > 0)
            {
                _maxSum += arr[j];
            }
        }
        Console.WriteLine($"{_minSum} {_maxSum}");
    }
    
  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/2GlHrf4HQPE

    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
        vector<int> arr(5);
        for(int i = 0; i < 5; i++) {
            cin >> arr[i];
        }
        auto min_max = minmax_element(arr.begin(), arr.end());
        long long su = 0;
        su = accumulate(arr.begin(), arr.end(), su);
        cout << su - *min_max.second  << " " << su - *min_max.first;
        return 0;
    }
    
  • + 0 comments
        arr.sort((a,b)=>a-b)
        
        console.log(arr.slice(0,arr.length-1).reduce((pv,cv)=>pv+cv), arr.slice(1,arr.length).reduce((pv,cv)=>pv+cv))
    

    Sort, Slice and Reduce