Mini-Max Sum

Sort by

recency

|

378 Discussions

|

  • + 0 comments

    Python

    def miniMaxSum(arr): 
    
        arr.sort() 
    
        mini = 0 
    
        maxi = 0 
    
        for i,j in enumerate(arr): 
    
            if i<len(arr)-1: 
    
                mini += j 
    
            if 0<i<len(arr): 
    
                maxi += j  
    
        print(mini, maxi)  
    
    minimax([4,5,1,3,9]) 
    
  • + 0 comments

    def miniMaxSum(arr): sums = [] for n in arr: total = sum(arr)-n sums.append(total) print(f"{min(sums)} {max(sums)}")

  • + 0 comments

    Tried all hard to get to this -

    using System.CodeDom.Compiler;
    using System.Collections.Generic;
    using System.Collections;
    using System.ComponentModel;
    using System.Diagnostics.CodeAnalysis;
    using System.Globalization;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    using System.Runtime.Serialization;
    using System.Text.RegularExpressions;
    using System.Text;
    using System;
    
    class Result
    {
        
         * The function accepts INTEGER_ARRAY arr as parameter.
         */
    
        public static void miniMaxSum(List<int> arr)
        {
            List<long> sumOfNumbers = new List<long>();
            for(int i =0; i < arr.Count(); i ++) {
                List<int> anotherAray = new List<int>(arr);
                anotherAray.RemoveAt(i);
                sumOfNumbers.Add(anotherAray.Sum(x => (long)x));
            }
            Console.WriteLine($"{sumOfNumbers.Order().First()} {sumOfNumbers.Order().Last()}");
        }
    }
    
    class Solution
    {
        public static void Main(string[] args)
        {
            List<int> arr = Console.ReadLine().TrimEnd().Split(' ').ToList().Select(arrTemp => Convert.ToInt32(arrTemp)).ToList();
    
            Result.miniMaxSum(arr);
        }
    }
    
  • + 0 comments

    TypeScript Solutions

    Solution 1

    function miniMaxSum(arr: number[]): void {
        const sortedArray = arr.sort((a, b) => a - b);
    		
        const totalSum = sortedArray.reduce((a, b) => a + b, 0);
    		
        const minSum = totalSum - sortedArray[arr.length - 1];
        const maxSum = totalSum - sortedArray[0];
    		
        console.log(minSum, maxSum);
    }
    
    // Simple, but with higher time complexity O(n log n)
    

    Solution 2

    function miniMaxSum(arr: number[]): void {
        let minNum = Number.MAX_SAFE_INTEGER;
        let maxNum = 1;
        let totalSum = 0;
    
        arr.forEach((num) => {
            totalSum += num;
            if (num < minNum) {
                minNum = num;
            }
            if (num > maxNum) {
                maxNum = num;
            }
        });
    
        console.log(totalSum - maxNum, totalSum - minNum);
    }
    
    // More complex, but with lower time complexity O(n)
    
  • + 0 comments

    How do you make sure the calculated sum is a 64 bit integer (since the sum might require more than 32 bits) without using numpy?