C++ || Maintain 2 arrays, front and back, to calculate the answer
In the solution, what I have tried to do is to have 2 arrays which store, for each index i, for the front array, the value stored in array tells us about the ma
In the solution, what I have tried to do is to have 2 arrays which store, for each index i, for the front array, the value stored in array tells us about the maximum window that can satisfy the given condition of k, and similarily for the back row, the values tell us about the maximum window size that satisfies the given condition.\n\nAnd, after we have calculated both front and back array, we just traverse from 0 to n-1 and use both these arrays to find 2 windows as required in the question.\n\n```\n\tint maximizeWin(vector<int>& arr, int k) {\n        int ans = 0, n = arr.size();\n        if(n==1) return 1;\n        \n        vector<int> fr(n, 0), bk(n, 0);\n        int i=0, j=0, mx = 1;\n        while(j<n) {\n            while(j>=i && arr[j]-arr[i] > k) i++;\n            mx = max(j-i+1, mx);\n            fr[j] = mx;\n            j++;\n        }\n        \n        j=n-1, i=n-1, mx = 1;\n        while(j>=0) {\n            mx = max(i-j+1, mx);\n            bk[j] = mx;\n            while(j<=i && arr[i]-arr[j] > k) i--;\n            j--;\n        }\n        \n        for(int i=0;i<n;i++) {\n            ans = max(ans, fr[i]+bk[i]-1);\n        }\n        \n        return ans;\n    }
['C']