C++ O(N) step by step


#1
vector<int> Solution::subUnsort(vector<int> &A) {
    int mnind = -1, mn;
	int mxind = -1, mx;
	for (int i = 0; i < A.size() - 1; i++)
		if (A[i] > A[i + 1])
		{
			mnind = i;
			mn = A[i + 1];
			break;
		}
	if (mnind < 0)
		return vector<int>{mnind};
	mx = mn;
	for (int i = mnind; i < A.size() - 1; i++)
		if (A[i] > A[i + 1])
		{
			mxind = i;
			mx = max(mx, A[i]);
			mn = min(mn, A[i + 1]);
		}
	int fin = -1;
	for (int i = mxind + 1; i < A.size(); i++)
		if (A[i] >= mx)
		{
			fin = i - 1;
			break;
		}
	if (fin ==-1)
		fin =  A.size() - 1;
	int st = mnind;
	for (int i = mnind; i > -1; i--)
		if (A[i] > mn)
			st = i;
	return vector<int>{st, fin};
}