Scan, count, search, and compare
Distinguish values from positions and preserve the exact tie rule a problem asks for.
A traversal visits each active cell
Use i to identify the cell and a[i] for the value. In [5, 2, 7, 8], values at even indices are 5 and 7, while the even values are 2 and 8. Read the wording carefully before deciding which quantity to test.
A sum can be wider than one element: 1000 copies of 10⁹ total 10¹². Keep that total in long long. A count of matching values adds one per match and cannot exceed n. A simple full traversal performs n visits.
The comparison decides ties
For a nonempty list, start minIndex and maxIndex at 0, then inspect indices 1 through n − 1. Compare a[i] with the candidate value, not with the candidate index. This works for all-negative inputs.
Updating a maximum on > retains the first occurrence. Updating on >= retains the last occurrence. Neither rule is universally right: the output specification determines which one to use.
Search answers different questions
A linear search checks successive values and does not require sorted data. A first-occurrence search saves i and breaks; a last-occurrence search keeps updating; an occurrence count increments for every match; existence only needs a bool.
Use −1 as a not-found marker because it cannot be a valid index. Never access a[answer] until you know answer is valid. If you need a one-based position, preserve the not-found marker rather than blindly adding one to −1.
#include <iostream>
int main() {
int a[1000]{};
int n = 0;
std::cin >> n;
for (int i = 0; i < n; ++i) { std::cin >> a[i]; }
int target = 0;
std::cin >> target;
int first = -1;
for (int i = 0; i < n; ++i) {
if (a[i] == target) {
first = i;
break;
}
}
std::cout << first << '\n';
return 0;
}When a second pass is the right tool
To count values above the final average, first store the list and calculate its sum; then revisit each value. The average is not yet known when the first value arrives.
For n > 0, a[i] > sum / n is mathematically equivalent to a[i] × n > sum, but integer division would truncate the first version. Under n ≤ 1000 and |a[i]| ≤ 10⁹, use 1LL * a[i] * n > sum for an exact, safe comparison. Two passes still perform work proportional to n.
For [6, 9, 9, 4], a maximum-index scan updates on >=. Which zero-based index remains?
Turn the idea into code
Read 1 ≤ n ≤ 1000 values in [−10⁹, 10⁹]. Count those strictly above their final average. Include tests with all equal and negative values.
Give me a hint
Avoid dividing: multiply the candidate by the positive n using wide arithmetic.
Show the explained solution
The first pass computes the total and stores the values. The second applies the equivalent exact comparison. For [−10, −4, −4], the average is −6 and the answer is 2.
#include <iostream>
int main() {
int a[1000]{};
int n = 0;
std::cin >> n;
long long sum = 0;
for (int i = 0; i < n; ++i) {
std::cin >> a[i];
sum += a[i];
}
int count = 0;
for (int i = 0; i < n; ++i) {
if (1LL * a[i] * n > sum) { ++count; }
}
std::cout << count << '\n';
return 0;
}Your notes
Mark complete when you can explain it yourself.