5ms 98% Faster O(n)-Dp+sliding-window Solution
java code is:\n# \n\nclass Solution {\n    public int minSumOfLengths(int[] arr, int target) {\n        int sum=0,i=0,n=arr.length,res=Integer.MAX_VALUE,length=
java code is:\n# \n```\nclass Solution {\n    public int minSumOfLengths(int[] arr, int target) {\n        int sum=0,i=0,n=arr.length,res=Integer.MAX_VALUE,length=1000000;\n        int dp[]=new int[n];\n        Arrays.fill(dp,-1);\n        for(int j=0;j<n;j++){\n            sum+=arr[j];\n            while(sum>target) {sum-=arr[i];\n                               if(dp[i]!=-1)length=Math.min(length,i-dp[i]+1);\n                               i++;    \n            }\n            if(sum==target){ \n                dp[j]=i;\n                if(length!=1000000)res=Math.min(res,length+j-i+1);\n            }\n        }\n        return res==Integer.MAX_VALUE ? -1 : res;\n    }\n}\n```\n**Time:** O(n)\n**Space:** O(n)\n***Please,Upvote if this is helpful***
[]