[JAVA] - Prefix Suffix Sum
\nclass Solution {\n    public int minSumOfLengths(int[] arr, int target) {\n        int[] left = findMinLengths(0, arr.length, target,arr);\n        int[] righ
```\nclass Solution {\n    public int minSumOfLengths(int[] arr, int target) {\n        int[] left = findMinLengths(0, arr.length, target,arr);\n        int[] right = findMinLengths(arr.length-1, 0,target,arr);\n        \n        for(int i=1;i<arr.length;i++)\n            left[i] = Math.min(left[i],left[i-1]);\n        \n        for(int i=arr.length-2;i>=0;i--)\n            right[i] = Math.min(right[i],right[i+1]);\n        \n        int min = arr.length+1;\n        \n        for(int i=0;i<arr.length-1;i++)\n            min = Math.min(min,left[i] + right[i+1]);\n        \n        return min == arr.length+1 ? -1 : min;\n    }\n    \n    private int[] findMinLengths(int start,int end,int target,int[] arr){\n        int[] ans = new int[arr.length];\n        \n        Arrays.fill(ans,arr.length);\n        \n        int i = start,j = start;\n        int sum = 0;\n        \n        int dir = (int)Math.signum(end-start);\n        \n        while(j != end){\n            sum += arr[j];\n            \n            while(sum > target){\n                sum -= arr[i];\n                i += dir;\n            }\n                \n            if(sum == target){\n                ans[j] = Math.abs(i-j)+1;\n            }\n            \n            j+=dir;\n        }\n        \n        return ans;\n    }\n}\n```
['Prefix Sum', 'Java']