[JAVA] O(N) Time, Two Pass Solution using HashMap.
Concept: First traverse through the array once and store the (key,value) pair as (sum(arr[0:i+1]),i) for 0<=i<size of arr. Put, (0,-1) in hashmap as default. No
Concept: First traverse through the array once and store the (key,value) pair as (sum(arr[0:i+1]),i) for 0<=i<size of arr. Put, (0,-1) in hashmap as default. Now traverse through the array again, and for every i, find the minimum value of length of sub-array on the left or starting with i whose value is equal to target. Find another sub-array starting with i+1, whose sum is target. Update the result with the minimum value of the sum of both the sub-array. This is possible because all values are positive and the value of sum is strictly increasing.\n```\nclass Solution {\n    public int minSumOfLengths(int[] arr, int target) {\n        HashMap<Integer,Integer> hmap=new HashMap<>();\n        int sum=0,lsize=Integer.MAX_VALUE,result=Integer.MAX_VALUE;\n        hmap.put(0,-1);\n        for(int i=0;i<arr.length;i++){\n            sum+=arr[i];\n            hmap.put(sum,i); // stores key as sum upto index i, and value as i.\n        }\n        sum=0;\n        for(int i=0;i<arr.length;i++){\n            sum+=arr[i];\n            if(hmap.get(sum-target)!=null){\n                lsize=Math.min(lsize,i-hmap.get(sum-target));      // stores minimum length of sub-array ending with index<= i with sum target. This ensures non- overlapping property.\n            }\n\t\t\t//hmap.get(sum+target) searches for any sub-array starting with index i+1 with sum target.\n            if(hmap.get(sum+target)!=null&&lsize<Integer.MAX_VALUE){\n                result=Math.min(result,hmap.get(sum+target)-i+lsize); // updates the result only if both left and right sub-array exists.\n            }\n        }\n        return result==Integer.MAX_VALUE?-1:result;\n    }\n}\n```
[]