Java Two Pointer | O(n)
\nclass Solution {\n    public int minSumOfLengths(int[] arr, int target) {\n        int n = arr.length,res = Integer.MAX_VALUE;\n        int[] dp = new int[n+1
```\nclass Solution {\n    public int minSumOfLengths(int[] arr, int target) {\n        int n = arr.length,res = Integer.MAX_VALUE;\n        int[] dp = new int[n+1];\n        Arrays.fill(dp,n);\n        int i = -1,j = -1,sum = 0;\n        while(j < n){\n            if(j < n-1 && sum < target) sum += arr[++j];\n            else if(i < n-1 && sum > target) sum -= arr[++i];\n            else if(sum == target){\n                if(dp[i+1] < n)\n                    res = Math.min(res,dp[i+1]+j-i);\n                dp[j+1] = j-i; \n                sum -= arr[++i];\n            }\n            else break;\n            dp[j+1] = Math.min(dp[j],dp[j+1]);\n        }\n        return res == Integer.MAX_VALUE?-1:res;\n    }\n}\n```
[]