Codeforces Round #839 (Div. 3) 1772D - Absolute Sorting Solution
Problem Link: https://codeforces.com/contest/1772/problem/D
Solution in C++:
- /// La ilaha illellahu muhammadur rasulullah
- ///******Bismillahir-Rahmanir-Rahim******///
- /// Abul Hasnat Tonmoy
- /// Department of CSE,23rd batch
- /// Islamic University,Bangladesh
- ///**********ALLAH IS ALMIGHTY************///
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long int
- int main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- int t;
- cin >> t;
- while (t--) {
- int n;
- cin >> n;
- std::vector<int> v(n);
- for (int i = 0; i < n; i++) cin >> v[i];
- int low = 0, high = 1e9;
- for (int i = 0; i < n - 1; i++) {
- if (v[i] == v[i + 1])
- continue;
- else if (v[i] < v[i + 1]) {
- int x = (v[i] + v[i + 1]) / 2;
- high = min(high, x);
- } else {
- int x = (v[i] + v[i + 1] + 1) / 2;
- low = max(low, x);
- }
- }
- if (low <= high)
- cout << high << endl;
- else
- cout << "-1" << endl;
- }
- }
No comments