Almost Sorted Time : O(nlogn) Space : O(n) 123456789101112131415161718192021222324252627282930void almostSorted(vector<int> A) { int n = A.size(), l = 0, r = n - 1; auto S = A; sort(begin(S), end(S)); while(l < n and A[l] == S[l]) l++; if(l == n) { cout<<"yes"<<endl; return; } while(r >= 0 and A[r] == S[r]) r--; int unmatch = 0; for(int i = l; i <= r; i++) { if(A[i] != S[i]) unmatch++; } if(unmatch == 2) { cout<<"yes"<<endl; cout<<"swap "<<l + 1<<' '<<r + 1<<endl; return; } for(int i = l, j = r; i <= r; i++,j--) { if(A[j] != S[i]) { cout<<"no"<<endl; return; } } cout<<"yes"<<endl; cout<<"reverse "<<l + 1<<' '<<r + 1<<endl;}