JAVA | Sliding window with only one array | No HasMap
I see, mot of the people confused this with 560. Subarray Sum Equals K off course that would give correct answer, no doubt about that. But I think we can levera
I see, mot of the people confused this with 560. `Subarray Sum Equals K` off course that would give correct answer, no doubt about that. But I think we can leverage the constraint here, which says `a[i] >= 1` due to this constraint we don\'t need a `HashMap` here. Problem can be solved by vanilla sliding window.\n\n ```\n    public int minSumOfLengths(int[] arr, int target) {\n        int n = arr.length;\n        int best[] = new int[n];\n        Arrays.fill(best, Integer.MAX_VALUE);\n        int sum = 0, start = 0, ans = Integer.MAX_VALUE, bestSoFar = Integer.MAX_VALUE;\n        for(int i = 0; i < n; i++){\n            sum += arr[i];\n            while(sum > target){\n                sum -= arr[start];\n                start++;\n            }\n            if(sum == target){\n                if(start > 0 && best[start - 1] != Integer.MAX_VALUE){\n                    ans = min(ans, best[start - 1] + i - start + 1);\n                }\n                bestSoFar = min(bestSoFar, i - start + 1);\n            }\n            best[i] = bestSoFar;\n        }\n        return ans == Integer.MAX_VALUE ? -1 : ans;\n    }\n```
[]