Mini-Max Sum Problem

Problem statement
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 i…


This content originally appeared on DEV Community and was authored by Mohamed Shawky

Problem statement
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.

Solution steps:

I thought about the problem mathematically that the sum of the smallest number in a set is the minimum sum and the sum of the biggest numbers in a set is the maximum sum so:

  • Sorted the given array.

  • Get the minimum sum by the summation of the first four numbers.

  • Get the maximum sum by the summation of the last four numbers.

def miniMaxSum(arr):
    sorted_list = sorted(arr)
    min_sum = sorted_list[0] + sorted_list[1] + sorted_list[2] + sorted_list[3]
    max_sum = sorted_list[1] + sorted_list[2] + sorted_list[3] + sorted_list[4]
    print(str(min_sum) + " " + str(max_sum))

The problem is from Hackerrank website.
The problem url: Mini-Max Sum


This content originally appeared on DEV Community and was authored by Mohamed Shawky


Print Share Comment Cite Upload Translate Updates
APA

Mohamed Shawky | Sciencx (2022-04-08T01:27:20+00:00) Mini-Max Sum Problem. Retrieved from https://www.scien.cx/2022/04/08/mini-max-sum-problem/

MLA
" » Mini-Max Sum Problem." Mohamed Shawky | Sciencx - Friday April 8, 2022, https://www.scien.cx/2022/04/08/mini-max-sum-problem/
HARVARD
Mohamed Shawky | Sciencx Friday April 8, 2022 » Mini-Max Sum Problem., viewed ,<https://www.scien.cx/2022/04/08/mini-max-sum-problem/>
VANCOUVER
Mohamed Shawky | Sciencx - » Mini-Max Sum Problem. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/04/08/mini-max-sum-problem/
CHICAGO
" » Mini-Max Sum Problem." Mohamed Shawky | Sciencx - Accessed . https://www.scien.cx/2022/04/08/mini-max-sum-problem/
IEEE
" » Mini-Max Sum Problem." Mohamed Shawky | Sciencx [Online]. Available: https://www.scien.cx/2022/04/08/mini-max-sum-problem/. [Accessed: ]
rf:citation
» Mini-Max Sum Problem | Mohamed Shawky | Sciencx | https://www.scien.cx/2022/04/08/mini-max-sum-problem/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.