Codeforces Round 552 (Div. 3) 1154B - Make Them Equal Solution


 

Problem Link :   https://codeforces.com/contest/1154/problem/B

Solution in C++:

  1. /// Author : AH_Tonmoy
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. int32_t main() {
  5. ios_base::sync_with_stdio(0);
  6. cin.tie(0);
  7. int n;
  8. cin >> n;
  9. set<int> st;
  10. int x;
  11. for (int i = 0; i < n; i++) {
  12. int x;
  13. cin >> x;
  14. st.insert(x);
  15. }
  16. if (st.size() > 3) {
  17. cout << "-1\n";
  18. } else if (st.size() == 1) {
  19. cout << "0\n";
  20. } else if (st.size() == 2) {
  21. std::vector<int> v;
  22. for (auto i : st) {
  23. v.push_back(i);
  24. }
  25. if ((v[1] - v[0]) % 2 == 0) {
  26. cout << (v[1] - v[0]) / 2 << "\n";
  27. } else
  28. cout << v[1] - v[0] << "\n";
  29. } else if (st.size() == 3) {
  30. std::vector<int> v;
  31. for (auto i : st) {
  32. v.push_back(i);
  33. }
  34. if ((v[1] - v[0]) == v[2] - v[1]) {
  35. cout << v[1] - v[0] << "\n";
  36. } else
  37. cout << "-1\n";
  38. }
  39. return 0;
  40. }

No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h &g...

Powered by Blogger.