TechoSagar: Maximum and Minimum Of Array Elements

Search

Google Alert - jobs

Maximum and Minimum Of Array Elements

Given an array, find maximum and minimum elements from the array.
Input:
The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains a single integer N denoting the size of array. The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of the array.
Output:
Print the maximum and minimum element in a single line with space in between.
Constraints:
1 ≤ T ≤ 30
1 ≤ N ≤ 100
0 ≤A[i]<100
Example:
Input:
2
4
5 4 2 1
1
8
Output:
5 1
8 8
Your Code
#include<iostream>

using namespace std;

int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int a[100];
int i;
for(i=0; i<n; i++)
{
cin>>a[i];
}
int max=a[0], min=a[0];
for(i=1; i<n; i++)
{
if(max<a[i])
{
max=a[i];
}
if(min>a[i])
{
min=a[i];
}
}
cout<<max<<" "<<min<<endl;
}
return 0;
}

Follow us

Follow Bijendra Kumar on Facebook