Editorial for Đếm số
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
Spoiler Alert
Hint 1
- Duyệt qua mảng
Khởi tạo biến \(dem = 0\)
Duyệt qua từng phần tử \(a_i\) và tăng biến đếm \(dem\) khi \(a_i = x\)
Hint 2
- Online Solving: Bạn có thể không cần lưu mảng
Reference AC code | \(O(n)\) time | \(O(1)\) auxiliary space | Online Solving, Implementation
C++
int n, x;
cin >> n >> x;
int dem = 0;
for (int i = 0; i < n; ++i) {
int t;
cin >> t;
dem += (x == t);
}
cout << dem;
Comments