A - Triple Four Editorial by en_translator
If you are new to learning programming and do not know where to start, please try Problem A “Welcome to AtCoder” from practice contest. There you can find a sample code for each language.
Also, if you are not familiar with problems in programming contests, we recommend you to try some problems in “AtCoder Beginners Selection” (https://atcoder.jp/contests/abs).
Let us try determining whether “there exists an integer \(i\) between \(1\) and \(N-2\), inclusive, such that \(A_i=A_{i+1}=A_{i+2}\),” as written after “more formally.”
Many programming languages employ \(0\)-based indexing, that is, the first element of an array \(A\) is specified by \(A[0]\) instead of \(A[1]\), so the condition is translated into code as “whether there exists an integer \(i\) between \(0\) (inclusive) and \(N-2\) (exclusive), such that \(A_i=A_{i+1}=A_{i+2}\).”
This can be done by the for
statement.
There are several ways to actually implement this algorithm. For example, while iterating \(i\) with a for
statement, one can:
- print
Yes
and terminate the program once a conforming \(i\) is found. If the program is not terminated after completing thefor
statement, it means that the answer isNo
, so printNo
. - implement the decision problem as a function or a method. Once such an \(i\) is found, return
Yes
(or something equivalent, like True). If the function proceeds after completing thefor
statement, it means that the answer isNo
, so returnNo
(or something equivalent, like False).
This way, your implementation will be short and concise.
The following is sample code in C++ and Python for each implementation strategy.
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n - 2; i++) {
if (a[i] == a[i + 1] && a[i + 1] == a[i + 2]) {
cout << "Yes" << endl;
return 0;
}
}
cout << "No" << endl;
return 0;
}
n = int(input())
a = list(map(int, input().split()))
for i in range(n - 2):
if a[i] == a[i + 1] == a[i + 2]:
print("Yes")
exit()
print("No")
#include <bits/stdc++.h>
using namespace std;
bool judge(int n, vector<int> a) {
for (int i = 0; i < n - 2; i++) {
if (a[i] == a[i + 1] && a[i + 1] == a[i + 2]) {
return true;
}
}
return false;
}
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
if (judge(n, a)) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
}
def judge(n, a):
for i in range(n - 2):
if a[i] == a[i + 1] == a[i + 2]:
return True
return False
n = int(input())
a = list(map(int, input().split()))
if judge(n, a):
print("Yes")
else:
print("No")
posted:
last update: