The problem requires counting amazing performances where a contestant's score breaks their
previous records. We initialize
minScore
and
maxScore
with the first
contest's score. For each subsequent score, we check if it either
exceeds maxScore
or
falls below minScore
.
When either condition is met, we update the corresponding extreme value
and increment our amazingCount.
#include <bits/stdc++.h> using namespace std; int main(){ int noOfContests; int score,refScore; int noOfAmazingPerformance=0; cin >> noOfContests; cin >> refScore; int minScore = refScore; int maxScore = refScore; for(int contest=1;contest<noOfContests;contest++){ cin >> score; if(score > maxScore){ maxScore = score; noOfAmazingPerformance++; } if(score < minScore){ minScore = score; noOfAmazingPerformance++; } } cout << noOfAmazingPerformance << endl; return 0; }
The algorithm iterates through the array of scores once, making it linear in time complexity.
The algorithm uses a constant amount of extra space, regardless of the input size.