A - Tiny Arithmetic Sequence Editorial by en_translator
Solution 1
There are \(6\) permutations of \((A_1,A_2,A_3)\). By checking for each permutation if it forms an arithmetic sequence, the problem can be solved.
Sample Code (C++)
#include<bits/stdc++.h>
using namespace std;
int main(){
int A1,A2,A3; cin >> A1 >> A2 >> A3;
string ans = "No";
//(A1,A2,A3)
if(A3-A2 == A2-A1) ans = "Yes";
//(A1,A3,A2)
if(A2-A3 == A3-A1) ans = "Yes";
//(A2,A1,A3)
if(A3-A1 == A1-A2) ans = "Yes";
//(A2,A3,A1)
if(A1-A3 == A3-A2) ans = "Yes";
//(A3,A1,A2)
if(A2-A1 == A1-A3) ans = "Yes";
//(A3,A2,A1)
if(A1-A2 == A2-A3) ans = "Yes";
cout << ans << endl;
}
Also, by the fact that when \((a,b,c)\) is an arithmetic sequence then \((c,b,a)\) is also an arithmetic sequence, one can reduce the number of conditional branches to three.
Sample Code (Python)
A1,A2,A3 = map(int,input().split())
ans = "No"
#(A1,A2,A3)
if A3-A2 == A2-A1:
ans = "Yes"
#(A1,A3,A2)
if A2-A3 == A3-A1:
ans = "Yes"
#(A2,A1,A3)
if A3-A1 == A1-A2:
ans = "Yes"
print(ans)
Solution 2
An arithmetic sequence must be monotonically increasing or decreasing, because the sign (positive, negative, or zero) of \((b-a)\) and \((c-b)\) must be the same. Therefore, you can easily get AC by first receiving \((A_1,A_2,A_3)\) as a sequence and sort in the increasing or decreasing order, and then check if it forms an arithmetic sequence.
Sample Code (C++)
#include<bits/stdc++.h>
using namespace std;
int main(){
vector<int> A(3);
cin >> A[0] >> A[1] >> A[2];
sort(A.begin(),A.end());
string ans = "No";
if(A[2]-A[1] == A[1]-A[0]) ans = "Yes";
cout << ans << endl;
}
Sample Code (Python)
A = list(map(int,input().split()))
A.sort()
ans = "No"
if A[2]-A[1] == A[1]-A[0]:
ans = "Yes"
print(ans)
posted:
last update: