Mini-Max Sum

  • + 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)
    }