Sum of Maximum and Minimum Elements Problem Statement

Given an array ARR of size N, your objective is to determine the sum of the largest and smallest elements within the array.

Follow Up:

Can you achieve the above task using the least number of comparisons possible?

Input:

The first line contains an integer T, the number of test cases.
For each test case, the first line contains an integer N, representing the size of the array ARR.
The second line contains N space-separated integers, which are the elements of array ARR.

Output:

For each test case, output the sum of the maximum and minimum elements in the array ARR.
Example:
Input:
2
5
1 3 5 7 9
4
-10 4 7 19
Output:
10
9
Explanation:

In the first test case, the maximum is 9 and the minimum is 1. Their sum is 10.
In the second test case, the maximum is 19 and the minimum is -10. Their sum is 9.

Constraints:

  • 1 ≤ T ≤ 10
  • 1 ≤ N ≤ 10^5
  • -10^9 ≤ ARR[i] ≤ 10^9

Note:

You are not required to print anything; the function should handle processing the results.
Be the first one to answer
Add answer anonymously...
SAP Senior Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter