Codeforces Round 865 (Div. 2) 1816C. Ian and Array Sorting Solution
Problem Link : https://codeforces.com/contest/1816/problem/C
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 t;
- cin >> t;
- while (t--) {
- int n;
- cin >> n;
- vector<long long> a(n);
- for (int i = 0; i < n; i++) {
- cin >> a[i];
- }
- for (int i = 1; i < n - 1; i++) {
- if (a[i] < a[i - 1]) {
- a[i + 1] +=(a[i - 1] - a[i]);
- a[i] = a[i - 1];
- }
- }
- for (int i = n - 2; i > 0; i--) {
- if (a[i] > a[i + 1]) {
- a[i - 1] -= (a[i] - a[i + 1]);
- a[i] = a[i + 1];
- }
- }
- if (is_sorted(a.begin(), a.end()))
- cout << "YES" << endl;
- else
- cout << "NO" << endl;
- }
- }
No comments