ronith
6/1/2018 - 5:41 PM

Cheapest Subarray

In this problem, you will be given an array of integers and you need to tell the cost of the cheapest possible subarray of length at least two.

A subarray is the sequence of consecutive elements of the array and the cost of a subarray is the sum of minimum and the maximum value in the subarray.

Constraints:

Input Format:

The first line contains a single integer denoting the number of test cases.

The first line of each test case contains i.e the number of elements in the array. Next lines contains space-separated integers

Output Format:

Print lines each containing a single integer. integer denotes the cost of the cheapest subarray for the array.

SAMPLE INPUT 2 2 3 2 3 3 4 2 SAMPLE OUTPUT 5 6

Idea:

#include<bits/stdc++.h>
using namespace std;

int main(){
    int t;
    cin >> t;
    while(t>0){
        int n;
        cin >> n;
        int a[n];
        for (int i=0;i<n;i++){
            cin >> a[i];
        }
        int min=INT_MAX, max=INT_MIN;
        for (int i=0;i<n;i++){
            if(a[i] < min)
                min =a[i];
            if (a[i]>max)
                max = a[i];
        }
        cout <<min+max;
    }
}