count_if, any_of, all_of
Completed
Read n integers. Print how many are even (count_if), whether any is negative (any_of) and whether all are below 100 (all_of).
Input
Line 1: n. Line 2: n integers.
Output
Three lines.
Example
Input
6 4 7 -2 10 99 3
Output
3 true true
Editor
C++
Hints
The lambda is a predicate: it returns true or false for each element.
Solution explanation
Named algorithms say what you're checking, which reads better than a loop.
Show solution
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> v(n);
for (int &x : v) cin >> x;
cout << count_if(v.begin(), v.end(), [](int x) { return x % 2 == 0; }) << "\n";
cout << boolalpha << any_of(v.begin(), v.end(), [](int x) { return x < 0; }) << "\n";
cout << all_of(v.begin(), v.end(), [](int x) { return x < 100; }) << "\n";
return 0;
}