C++ Two pointer O(N)
Intuition\nlet\'s consider three simple cases!\n\nCase 1: When the sorted sublist is at the beginning\narr[] = {1, 2, 3, 4, 5, 4, 2} -> for this example last 2 
Intuition\nlet\'s consider three simple cases!\n\nCase 1: When the sorted sublist is at the beginning\narr[] = {1, 2, 3, 4, 5, 4, 2} -> for this example last 2 elements should be deleted.\n\nCase 2: When the sorted sublist is at the end.\narr[] = {4, 2, 1, 2, 3, 4 , 5} -> for this example first 2 elements should be deleted.\n\nCase 3: To get the sorted list we need to attach the few elements from the beginning, and few elements(contiguous) from the end.\narr[] = {1, 1, 3, 5, 4, 2, 1, 9} -> Here, first elements 1, 1, 3, 5 should be attached with the last element 9 to get the minimum sublist.\n\nConsidering the three cases, we need to have prefix of sorted and suffix of sorted and all comparisons to merge them and to get to the minimal solution.\n\nHere are the comparisons,\nFor the first case, to get the answer, we have prefix of length 5,\ntotal length of array 7\nres -> 7 - 5 = 2\n\nFor the second case it is same as the first\n\nFor the third case, in order to combine the prefix and suffix,\ni -> pointer in the prefix.\nj -> pointer in the suffix.\nnums[j] >= nums[i] should hold true.\nWe compare all possible combinations of prefix and suffix and arrive at the minimal.\n\nImplementation\nThere are few corner cases in the implementation:\n\nif the prefix runs till the end means it is completely sorted, in which case we should return true.\nFor every comparison, we should check if it yeilds the minimum output.\n\n\ncode-\nint ans=0;\n    int n=arr.size();\n    if(n==0)return 0;\n    if(n==1)return 0;\n\n    int pre=0;\n    for(int i=0;i<n-1;i++){\n            if(arr[i]<=arr[i+1])pre++;\n            else break;\n    }\n\n    if(pre==n-1)return 0;\n    int suff=n-1;\n    for(int j=n-2;j>=0;j--){\n            if(arr[j]<=arr[j+1])suff--;\n            else{\n                    break;\n            }\n    }\n\n    int i=0;\n    int j=suff;\n    int res=min(n-pre-1,suff);\n    while(i<=pre && j<=n-1){\n            if(arr[i]<=arr[j]){\n                    res=min(res,j-i-1);\n                    i++;\n            }else{\n                    j++;\n            }\n    }\n\n\n\n    return res;\n\n\n\n   \n
['C']