Mini-Max Sum

Sort by

recency

|

929 Discussions

|

  • + 0 comments

    my solution (Python):

    def miniMaxSum(arr):
        # Write your code here
        sums = []
        for i in range(len(arr)):
            sums.append(sum(arr) - arr[i])
        print(f"{min(sums)} {max(sums)}")
    
  • + 0 comments

    If some one is looking for a go solution with quick sort method, here is mine (working for all size of array) :

    func miniMaxSum(arr []int32) {
        
        // Sort array with Quick Sort method
        var sortArray func (arrSort []int32) []int32
        
        sortArray = func(arrToSort []int32) []int32 {
            if len(arrToSort) <= 1 {
                return arrToSort
            }
            pivot := arrToSort[len(arrToSort)-1]
            arrToSort = arrToSort[:len(arrToSort)-1]
            
            var smallerArr, biggerArr []int32
            
            for _, v := range arrToSort {
                if v < pivot {
                    smallerArr = append(smallerArr, v)
                } else {
                    biggerArr = append(biggerArr, v)
                }
            }
            smallerArr = append(sortArray(smallerArr), pivot)
            
            return append(smallerArr,sortArray(biggerArr)...)
        }
        
        arrSorted := sortArray(arr)
        
        // Calcule Minimun and Maximum 
        var minSum, maxSum int64
        
        for i, v := range arrSorted {
            if i >= 1 {
                maxSum = maxSum + int64(v)
            }
            if i <= len(arrSorted)-2 {
                minSum = minSum + int64(v)
            }
        }
    		
        
        fmt.Printf("%d %d",minSum, maxSum)
    }
    
  • + 1 comment

    Python solution (I tried to avoid loops, just for fun)

    def miniMaxSum(arr): arr.sort() middle = sum(arr[i] for i in range(1,4)) minv = arr[0] + middle maxv = middle + arr[4]

    print(minv, maxv) 
    

    if name == 'main':

    arr = list(map(int, input().rstrip().split()))
    
    miniMaxSum(arr)
    
  • + 1 comment

    In c++ i get this 2063136757 18446744072159051664 from 256741038 623958417 467905213 714532089 938071625 and i can't figure out why.....

  • + 0 comments

    I'm a newby but this one made me a bit happy... :)

    // Javascript function miniMaxSum(arr) { // Write your code here

    // console.log(arr)

    let totalArray = [];

    for(let i = 0; i < arr.length; i++){

    let sum = arr.reduce((acc, e) => acc + e,0) - arr[i];
    
    totalArray.push(sum);
    
     //console.log(sum)
    
    }
    

    console.log({Math.max(...totalArray)})

    }