count, find, accumulate
Completed
Read n integers and a value x. Print how many times x appears (count), the index of its first occurrence or -1 (find), and the sum (accumulate).
Input
n, the integers, then x.
Output
Three lines.
Example
Input
6 4 2 4 9 4 1 4
Output
3 0 24
Editor
C++
Hints
Algorithms take a [begin, end) iterator range.
find returns end() when not found.
Solution explanation
Pass 0LL to accumulate so the sum is a long long.
Show solution
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
int main() {
int n, x;
cin >> n;
vector<int> v(n);
for (int &e : v) cin >> e;
cin >> x;
auto it = find(v.begin(), v.end(), x);
cout << count(v.begin(), v.end(), x) << "\n";
cout << (it == v.end() ? -1 : (int) (it - v.begin())) << "\n";
cout << accumulate(v.begin(), v.end(), 0LL) << "\n";
return 0;
}