O(3n/2) comparisons


#1

Since it was asked to reduce the number of comparisons, Solution with O(3n/2) comparisons and runtime complexity O(n)

def solve(self, A):
        
        if len(A) == 1:
            return 2 * A[0]
        elif len(A) == 2:
            return A[0] + A[1]
        
        if A[0] > A[1]:
            ma = A[0]
            mi = A[1]
        else:
            mi = A[0]
            ma = A[1]
        for i in range(2, len(A) - 1):
            
            if A[i] < A[i + 1]:
                x = A[i]
                y = A[i + 1]
            else:
                y = A[i]
                x = A[i + 1]
            
            if x < mi:
                mi = x
            if y > ma:
                ma = y
        return mi + ma