Java | heap | correct for given cases | feel free to modify to run all test case
\nfind all subarrays with given sum. and add it to heap ( sorted according to the length of subarray).\ntake 2 subarrays out. if they overlap. insert min one an
\nfind all subarrays with given sum. and add it to heap ( sorted according to the length of subarray).\ntake 2 subarrays out. if they overlap. insert min one and continue till u get result.\n\n**doesnt work for test cases where u need to not put min back inside the heap.**\n\n```\nclass Solution {\n    public int minSumOfLengths(int[] arr, int target) {\n        Map<Integer, Integer> m = new HashMap<>();\n       \n        Queue<int[]> q = new PriorityQueue<>(new Comparator<int[]>(){\n            @Override\n            public int compare(int[] x, int[] y)\n            {\n                return x[0]-y[0];\n            }\n        });\n        int sum =0;\n        m.put(0,-1);\n        for(int i=0; i<arr.length; i++)\n        {\n            sum +=arr[i];\n            if(m.containsKey(sum-target))\n            {\n                \n                q.add(new int[]{i-m.get(sum-target) , i});\n            }\n            m.put(sum, i);\n        }\n        while(q.size()>1)\n        {\n            int x[] = q.poll();\n            int y[] = q.poll();\n            if(overlap(x,y))\n                q.add(x);\n            else\n                return x[0]+y[0];\n        }\n        return -1;\n    }\n    boolean overlap(int x[], int[] y)\n    {\n        if(x[1]<=y[1]-y[0])\n            return false;\n        if(y[1]<=x[1]-x[0])\n            return false;\n        return true;\n        \n        \n    }\n}\n```
[]