-
Notifications
You must be signed in to change notification settings - Fork 353
/
Copy pathMin_Max_Sum.py
50 lines (39 loc) · 1.07 KB
/
Min_Max_Sum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
'''
Aim: Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers.
Example:
arr = [1,3,5,7,9]
The minimum sum is 1+3+5+7 = 16 and the maximum sum is 3+5+7+9 = 24. So, the output will be:
16 24
'''
def MMSum(a):
# sorting the array so that it becomes easier to find the min and max set of values
a.sort()
minn=0
maxx=0
for i in range(0,4):
# summing up all minimum values
minn+=a[i]
for i in range(1,5):
# summing up all maximum values
maxx+=a[i]
print(minn,maxx)
# getting the input
user_input = (input().strip().split())
array = []
for i in user_input:
array.append(int(i))
# calling the Min-Max-Sum function
MMSum(array)
'''
COMPLEXITY:
Time Complexity -> O(N)
Space Complexity -> O(N)
Sample Input:
2 1 3 4 5
Sample Output:
10 14
Explanation:
Sorted array: [1,2,3,4,5]
Min Sum: 1+2+3+4 = 10
Max Sum: 2+3+4+5 = 14
'''