-1

I Was Making A code For Making Sum Of list OF Max And Min. 4 Elements from a list of 5 Elemts . My COde Was Working Fine Until it is Introduced TO large No.'s .

def miniMaxSum(arr):
    sum1=0
    small=arr[0]+arr[1]+arr[2]+arr[3]+arr[4]
    lar=0
    for i in range(0,4):
        sum1=0
        for j in range(0,4):
            if i+j<=4:
                sum1+=arr[i+j]
            else:
                sum1+=arr[(i+j)-5]
        if sum1>=lar:
            lar=sum1
        if sum1<=small:
            small=sum1
    print("Minimum:",small,"Maximum:",lar)
if __name__ == '__main__':
    arr = list(map(int, input().rstrip().split()))
    miniMaxSum(arr)

I Am Getting Correct Answers if I am Using Small no. In Array .However it can't show Minimum Sum when Introduced to large No. example: 396285104 573261094 759641832 819230764 364801279 maximum is Working fine But Minimum is coming 2153578241 instead of 2093989309

Garvit Joshi
  • 103
  • 1
  • 10
  • use `print()` to see calculation in different places in code. OR learn how to use debugger. – furas Oct 12 '19 at 06:03
  • 1
    How about you just remove the smallest item to get the four largest? Or sort them and take the first four? – Klaus D. Oct 12 '19 at 06:05
  • 1
    normally I would use `sum(sorted(arr)[:4]), sum(sorted(arr)[1:])` but I expect you try to do this without some functions. – furas Oct 12 '19 at 06:06
  • thanks @furas `def miniMaxSum(arr): arr.sort() small=0 large=0 for i in range(0,4): small+=arr[i] for i in range(1,5): large+=arr[i] print(small,large) if __name__ == '__main__': arr = list(map(int, input().rstrip().split())) miniMaxSum(arr) ` – Garvit Joshi Oct 12 '19 at 06:14

2 Answers2

2

The issue your current code has isn't with large numbers, but with index 3. You never exclude that index from your calculations, so if it is the largest of smallest in your list, you'll get the wrong results.

The reason you never skip index 3 is that your outer loop runs is on range(0, 4), and the inner loop is too. That means you take four items, starting at indexes 0 through 3. But you never start at index 4, which is the situation where index 3 would be skipped.

So you can fix your code by making a simple fix:

 for i in range(0, 5):            # replace 4 with 5 here!
    sum1=0
    for j in range(0,4):
        ...

I'd note that there are many more efficient ways of solving this problem. For instance, you could compute the sum of all five values in the list (which you're already doing, in small, though using the builtin sum function would be easier), then find the single largest and smallest values (perhaps using the min and max functions). You can then find the smallest sum of four items by subtracting the largest single item from the sum of all the items, and the largest sum of four by subtracting the smallest single item instead.

def miniMaxSum(arr):
    arr_sum = sum(arr)
    min_val = min(arr)
    max_val = max(arr)
    print("Minimum:", arr_sum - max_val, "Maximum:", arr_sum - min_val)
Blckknght
  • 100,903
  • 11
  • 120
  • 169
1

Normally I would use

print("Minimum:", sum(sorted(arr)[:4]), "Maximum:", sum(sorted(arr)[1:]))

I tried understand your code but only I could change it to this:

def miniMaxSum(arr):
    small = arr[0] + arr[1] + arr[2] + arr[3] + arr[4]
    large = 0
    for i in range(5):
        temp = 0
        for j in range(5):
            if i != j:
                temp += arr[j]
        if temp > large:
            large = temp
        if temp < small:
            small = temp
    print("Minimum:", small, "Maximum:", large)

#arr = list(map(int, input().rstrip().split()))
miniMaxSum(arr)

Using i != j I always skip one number so it always sums only 4 numbers.

furas
  • 134,197
  • 12
  • 106
  • 148