A. Peak Finding
Author: Jishnu Roychoudhury (astoria)
Tester: Taranpreet Singh (taran_1407)
Editorialist: Jishnu Roychoudhury (astoria)
DIFFICULTY:
CAKEWALK
PREREQUISITES:
None
PROBLEM:
Find the maximum value in an array.
QUICK EXPLANATION:
Iterate through the array storing a maximum value in O(N).
EXPLANATION:
Initialise a “maximum so far” variable to 0. Use a for loop to iterate through the array. For each array element, set the “maximum so far” variable to the maximum of itself and the array element. Output the “maximum so far” variable at the end.
SOLUTIONS:
Setter's Solution
#include <bits/stdc++.h>
using namespace std;
void sol(){
int n;
cin >> n;
int a[n];
for (int i=0; i<n; i++) cin >> a[i];
int mx = 0;
for (int i=0; i<n; i++){
mx = max(mx, a[i]);
}
cout << mx << endl;
}
int main(){
int t; cin >> t; while(t--) sol();
}