Codeforces Round 552 (Div. 3) 1154B - Make Them Equal Solution
Problem Link : https://codeforces.com/contest/1154/problem/B
Solution in C++:
- /// Author : AH_Tonmoy
- #include <bits/stdc++.h>
- using namespace std;
- int32_t main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- int n;
- cin >> n;
- set<int> st;
- int x;
- for (int i = 0; i < n; i++) {
- int x;
- cin >> x;
- st.insert(x);
- }
- if (st.size() > 3) {
- cout << "-1\n";
- } else if (st.size() == 1) {
- cout << "0\n";
- } else if (st.size() == 2) {
- std::vector<int> v;
- for (auto i : st) {
- v.push_back(i);
- }
- if ((v[1] - v[0]) % 2 == 0) {
- cout << (v[1] - v[0]) / 2 << "\n";
- } else
- cout << v[1] - v[0] << "\n";
- } else if (st.size() == 3) {
- std::vector<int> v;
- for (auto i : st) {
- v.push_back(i);
- }
- if ((v[1] - v[0]) == v[2] - v[1]) {
- cout << v[1] - v[0] << "\n";
- } else
- cout << "-1\n";
- }
- return 0;
- }
No comments