In hackerrank,this question is failing 8 out of 15 testcases,can someone please correct this and tell me what is wrong.
Also I want to use random in this question and not by other methods.
import random
inputting values in list
arr=[int(x) for x in input().split()]
assigning a randomly selected large number for minimum (mini)
mini=1000000000000
maxi=0
for x in range(len(arr)-1):
randomsum=sum(random.sample(arr,4))
if randomsum<mini:
mini=randomsum
if randomsum >maxi:
maxi=randomsum
print(mini,maxi)
Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.
the problem clearly states that the printed values should be the 4 SMALLEST and the 4 LARGEST integers in the list. For greater sizes of the array, random.sample could still not cover all the parts of the array. The problem requires a concrete solution and not randomizing the sample list. 2 line solution:
def miniMaxSum(arr):
arr = sorted(arr)
print(sum(arr[0:4]), sum(arr[-4:]))
The reason why randomly getting a sample of size 4 is because the problem requires you to explicitly get the 4 smallest and 4 biggest numbers and print their sum on the screen, therefore if you randomize the sample it is unlikely that you will get the 4 smallest and 4 biggest numbers, even if you didn't randomize but traversed the list and compares every 4 lenght subList to the samllest and biggest integer, you wouldn't succeed still, because it is still unlikely that the list is already sorted and you would get the 4 smallest and 4 biggest numbers in order. That is why something like that will fail:
minSeq = max(arr) + 1
maxSeq = 0
for i in range(len(arr) - 4):
sample = sum(arr[i:i+4])
if sample < minSeq:
minSeq = sample
elif sample > maxSeq:
maxSeq = sample
print(minSeq, maxSeq)