Official

B - Failing Grade Editorial by en_translator


Whether a student failed or not can be determined by comparing which of \(a_i\) and \(P\) is larger with comparison operator > or < in an \(O(1)\) time.

Therefore, the problem can be solved in a total complexity of \(O(N)\) by preparing a variable \(\mathrm{ans}\) to store the answer, initialized to \(0\), and incrementing \(\mathrm{ans}\) by \(1\) if \(a_i \lt P\).

Sample codes in C++ and Python follow.

  • C++
#include <iostream>
using namespace std;

int main() {
  int N, P, ans = 0;
  cin >> N >> P;
  for (int i = 0, a; i < N; i++) {
    cin >> a;
    if (a < P) ans++;
  }
  cout << ans << "\n";
}
  • Python
N, P = map(int, input().split())
print(sum(x < P for x in map(int, input().split())))

posted:
last update: