HackerRank ‘Mini-Max Sum’ Solution

H
Short Problem Definition:

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.

Link

Mini-Max Sum

Complexity:

time complexity is O(N)

space complexity is O(1)

Execution:

Rather than recalculating the sum every time, keep track of the minimal element and maximal element. The result is the overall sum minus the min/max element.

Note: The editorial claims that the task is O(1), but I disagree. It is O(N).

Solution:
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
#!/bin/python
 
import math
import os
import random
import re
import sys
 
# Complete the miniMaxSum function below.
def miniMaxSum(arr):
    min_ele = sys.maxint
    max_ele = 0
    arr_sum = 0
 
    for val in arr:
        min_ele = min(min_ele, val)
        max_ele = max(max_ele, val)
        arr_sum += val
     
    return arr_sum-max_ele, arr_sum-min_ele
 
 
if __name__ == '__main__':
    arr = map(int, raw_input().rstrip().split())
 
    print " ".join(map(str, miniMaxSum(arr)))

About the author

Add comment

Archives

Categories