Maximum Minimum Sum

Last updated: 29th Aug, 2020

     

Problem Statment

Given an array A of size N . You need to find sum of maximum and minimum element in the given array. Note: you should make minimum number of comparisons.

Problem Constrains 1 <= N <= 105 -109 <= A[i] <= 109

Input Format
First and only argument is an integer array A of size N.

Output Format
Return an integer denoting the sum of maximum and minimum element in the given array.

Example 1:

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

>> 1

Example 2:

A=[1,3,4,1]

>> 5

Approach:

Use dynamic memory allocation and Standard Template Library(STL) Techniques to do it with the least number of comparisions.

C++ Code
class solution
{
public:
     int find_sum(vector & arr)
     {
        sort(arr.begin(), arr.end());
        return arr[0]+arr[arr.size()-1];
     }
 };