Help | Optimization Required | Sliding Window Algorithm | Java O(n^2) | TLE
I tried solving this problem using the sliding window algorithm. \nIt works fine for the example test cases as well as some of the test cases.\nPlease help me o
I tried solving this problem using the sliding window algorithm. \nIt works fine for the example test cases as well as some of the test cases.\nPlease help me optimize this solution, if optimisation is possible.\nThank you.\n\n```\nclass Solution {\n    public int minSubarray(int[] nums, int p) {\n        long sum =0;\n        for(int num: nums){\n            sum+=num;\n        }\n        \n        if(sum%p==0) return 0;\n        \n        long difference=sum%p; // we need to remove this from the array in order to make the total sum divisible by p\n        \n        //looping over all the possible lengths of window starting from 1 (as we need to find the minimum length required)\n        for(int ws=1;ws<nums.length-1;ws++){\n            if(check(nums,difference,ws,p)){\n                return ws; //if any required window is found, return the size of that window \n            }\n        }\n        \n        //if no window found return -1\n        return -1;\n    }\n    \n    \n    private boolean check(int[] nums, long req_sum, int win,int p){\n        long csum=0; // for storing the current sum in window\n        for(int i=0;i<win;i++){\n            csum+=nums[i];\n        }   \n        \n        if(csum==req_sum || (csum%p)==req_sum) // if initial sum is the required sum, return from here itself\n            return true;\n        \n        for(int i=win;i<nums.length;i++){ // sliding the window\n            csum+=nums[i]-nums[i-win];\n            \n            if(csum==req_sum||(csum%p)==req_sum) \n                return true;        \n        }\n        \n        //if no window is found, return false\n        return false;\n    }\n}\n```
['Sliding Window', 'Java']