problem no 36:(array problem):finding minimum and maximum element inside array.

 QUESTION:

Find minimum and maximum element in an array
Basic Accuracy: 81.18% Submissions: 1632 Points: 1

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

 

Example 1:

Input:
N = 6
A[] = {3, 2, 1, 56, 10000, 167}
Output:
min = 1, max =  10000

 

Example 2:

Input:
N = 5
A[]  = {1, 345, 234, 21, 56789}
Output:
min = 1, max = 56789
 
ANSWER:
pair<long long, long long> getMinMax(long long a[], int n) {
long long min=a[0];
long long max=a[0];

for(int i=0;i<n;i++)
{
if(a[i]>max)
{
max=a[i];
}
if(a[i]<min)
{
min=a[i];
}
}

pair<long long,long long> p;
p.first=min;
p.second=max;
return p;
}

 

Comments

Popular posts from this blog

problem 3: given two integers N and M. The problem is to find the number closest to N and divisible by M. If there are more than one such number, then output the one having maximum absolute value.

problem no 7:Given two numbers A and B, find Kth digit from right of AB.

Problem no 16: count the number of squares below N.