Skip to main content

Max Min

Problem​

Given an array A of size N of integers. Your task is to find the sum of minimum and maximum element in the array.

Examples:​

Example 1:

Input:
N = 5
A[] = {-2, 1, -4, 5, 3}
Output: 1
Explanation: min = -4, max = 5. Sum = -4 + 5 = 1

Example 2:

Input:
N = 4
A[] = {1, 3, 4, 1}
Output: 5
Explanation: min = 1, max = 4. Sum = 1 + 4 = 5

Your task:​

You don't need to read input or print anything. Your task is to complete the function findSum() which takes the array A[] and its size N as inputs and returns the summation of minimum and maximum element of the array.

  • Expected Time Complexity: O(N)O(N)
  • Expected Auxiliary Space: O(1)O(1)

Constraints:​

  • 1<=N<=1051<=N<=10^5
  • −109<=Ai<=109-10^9 <= A_i <= 10^9

Solution​

Python​

def findSum(self,A,N): 
A.sort()
minmax = A[0]+A[-1]
return minmax

Java​

public static int findSum(int A[],int N) {
Arrays.sort(A);
int minmax = A[0] + A[N - 1];
return minmax;
}

C++​

int findSum(int A[], int N) {
sort(A, A + N);
int minmax = A[0] + A[N - 1];
return minmax;
}

C​

void rotate(int arr[], int n) {
int last_el = arr[n - 1];
for (int i = n - 1; i > 0; i--)
arr[i] = arr[i - 1];
arr[0] = last_el;
}
  • Time Complexity: O(N)O(N)
  • Auxiliary Space: O(1)O(1)