With n number of comparision


#1

if the particular is greater than the previous max then it can’t be the minimum and vice versa hence it will take n comparison in for loop
int Solution::solve(vector &A) {
int n=A.size();
int max=A[0];
int min=A[0];
for(int i=1;i<n;i++)
{
if(max<A[i])
max=A[i];
else if(min>A[i])
min=A[i];
}
return (max+min);
}.


#2

Your code will take 2*n comparisons for the worst case of an array to be [5,4,3,2,1].