TechoSagar: Largest Element in ArrayLargest Element in Array

Search

Google Alert - jobs

Largest Element in ArrayLargest Element in Array

Given an array, find the largest element in it.
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer n , the no of elements in the array. Then next line contains n integers of the array.

Output:
Print the maximum element in the array.
Constraints:
1<=T<=100
1<=n<=100
1<=a[i]<=1000

Example:
Input:

2
5
10 324 45 90 9808
7
1 2 0 3 2 4 5
Output :
9808
5
Your code
#include<iostream>

using namespace std;

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

Follow us

Follow Bijendra Kumar on Facebook