Concise Java Solution
\nclass Solution {\n   public int minSumOfLengths(int[] arr, int target) {\n       int n = arr.length;\n       \n       //best is used to store the length of sh
```\nclass Solution {\n   public int minSumOfLengths(int[] arr, int target) {\n       int n = arr.length;\n       \n       //best is used to store the length of shortest sub-array with total sum of target up to i inclusively\n       int best[] = new int[n];\n       \n       //initialize the best array\n       Arrays.fill(best, Integer.MAX_VALUE);\n       \n       //for prefix sum\n       int sum = 0;\n       \n       //the starting index of the sliding window\n       int start = 0;\n       \n       //ans is to stored the result\n       int ans = Integer.MAX_VALUE;\n       \n       //bestSoFar is the length of shortest sub-array with total sum of target across arr\n       int bestSoFar = Integer.MAX_VALUE;\n       \n       //i is the ending index of the sliding window\n       for(int i = 0; i < n; i++){\n           //calculate the prefix sum up to i\n           sum += arr[i];\n           \n           //move the start to the left while sum > target\n           while(sum > target){\n               sum -= arr[start];\n               start++;\n           }\n           if(sum == target){\n               //the current sliding window is not the first window with the sum of target\n               if(start > 0 && best[start - 1] != Integer.MAX_VALUE){\n                    //current sliding window has the length of i - start + 1\n                    //previous shortest sliding window has the length of best[start - 1]\n                   ans = Math.min(ans, best[start - 1] + i - start + 1);\n               }\n               //to get the shortest length up to i\n               bestSoFar = Math.min(bestSoFar, i - start + 1);\n           }\n           best[i] = bestSoFar;\n       }\n       return ans == Integer.MAX_VALUE ? -1 : ans;\n   }\n}\n```
['Sliding Window']