Data Structure & Algorithms
DSA
Introduction to arrays
Max Min of Array

Max Min of an Array

Problem Description

Given an array A of size N. You need to find the sum of Maximum and Minimum element in the given array.

Problem Constraints

1 <= N <= 10^5
-109 <= A[i] <= 10^9

Input Format

First argument A is an integer array.

Output Format

Return the sum of maximum and minimum element of the array

Example Input

Input 1:
A = [-2, 1, -4, 5, 3]

Input 2:
A = [1, 3, 4, 1]

Example Output

Output 1:
1

Output 2:
5

Example Explanation

Explanation 1:
Maximum Element is 5 and Minimum element is -4. (5 + (-4)) = 1. 

Explanation 2:
Maximum Element is 4 and Minimum element is 1. (4 + 1) = 5

Output

Java
import java.util.Arrays;
 
public class MaxMinSum {
    public int findMaxMinSum(int[] A) {
        Arrays.sort(A);
        return A[0] + A[A.length - 1];
    }
}
Python
def find_max_min_sum(A):
    A.sort()
    return A[0] + A[-1]
JavaScript
function findMaxMinSum(A) {
    A.sort((a, b) => a - b);
    return A[0] + A[A.length - 1];
}