[C++] How to Approach and Logic
First of all this question seemed to me a dynamic problem. IDK why :(\n\nHere\'s my approach\n1.  First I thought to find incresing subsequnce from the front en
First of all this question seemed to me a dynamic problem. IDK why :(\n\nHere\'s my approach\n1.  First I thought to find incresing subsequnce from the front end and back end.\n2.  Either these subsequeces front and back can be of same length or one of them can be greater.\n3.  But subsequentially I arrived to the case where both the right and left can be same and concluded that thier lengths don\'t matter i.e. longer continuous increasing subsequence is at front or back.\n4.  We need to check two cases\n\tCase 1: \n\t\t\tIf front taken completely then what longest sequence do we get\n\tCase 2:\n\t\t\tIf the back is taken completely then what longest sequences do we get\n5. In both the case we calculate number of elements excluded and return the minimum number of exluded elements.\n\nHere is the code.\n\n\n```\nclass Solution\n{\npublic:\n    int findLengthOfShortestSubarray(vector<int> &arr)\n    {\n        int n = arr.size(), i;\n        int back = 1;\n        int front = 1;\n        for (i = 0; i < n - 1; i++)\n        {\n            if (arr[i] <= arr[i + 1])\n                front = i + 2;\n            else\n                break;\n        }\n        for (i = n - 1; i > 0; i--)\n        {\n            if (arr[i] >= arr[i - 1])\n                back = n - i + 1;\n            else\n                break;\n        }\n        //If the array is completely sorted\n        if (front == n)\n            return 0;\n        //If the array is sorted completely in reverse order\n        if (i == 0)\n            return n - 1;\n\n        //cout << back << " " << front;\n        // Intresting from here\n        int frontIndex = back, backIndex = front;\n        for (i = n - back; i < n; i++)\n            if (arr[i] < arr[front - 1])\n                frontIndex--;\n            else\n                break;\n\n        for (i = front - 1; i >= 0; i--)\n            if (arr[i] > arr[n - back])\n                backIndex--;\n            else\n                break;\n\n        return min(n - front - frontIndex, n - back - backIndex);\n    }\n};\n```\n\n
['C']