W and W/O Binary Search | 100% Faster | Commented
Solution 1:\n--\n\nclass Solution {\n    \n    public long minSum(int[] nums1, int[] nums2) {\n        long sum1 = 0l, sum2 = 0l;\n        int countZeroes1 = 0,
Solution 1:\n--\n```\nclass Solution {\n    \n    public long minSum(int[] nums1, int[] nums2) {\n        long sum1 = 0l, sum2 = 0l;\n        int countZeroes1 = 0, countZeroes2 = 0;\n        for(int num : nums1){\n            if(num==0)\n                countZeroes1++;\n            sum1 += (long)num;\n        }\n\t\t\n        for(int num : nums2){\n            if(num==0)\n                countZeroes2++;\n            sum2 += (long)num;\n        }\n\t\t\n\t\tsum1 += countZeroes1; // add the count of zeroes to array Sum, this is the minimum sum that we can have for array\n\t\tsum2 += countZeroes2;\n\t\t\n\t\t// now if sum of any array is less than the other one and its count of zeroes is 0, that means it cannot be changed \n\t\tif((sum1 < sum2 && countZeroes1==0) || (sum2 < sum1 && countZeroes2==0))\n            return -1;\n\t\t\t\n\t\t//else it is possible, Now the array having maximum minimum sum is our answer\n\t\t// because if you take this max sum we know the other array has lower sum and it is already filled with all 1\'s for zero\n\t\t// so its sum can be increased by incrementing the 1\'s\n        return Math.max(sum1, sum2);\n    }\n}\n``` \nSolution 2:\n--\n```\nclass Solution {\n    \n    public long minSum(int[] nums1, int[] nums2) {\n        long sum1 = 0l, sum2 = 0l;\n        int countZeroes1 = 0, countZeroes2 = 0;\n        for(int num : nums1){\n            if(num==0)\n                countZeroes1++;\n            sum1 += (long)num;\n        }\n        for(int num : nums2){\n            if(num==0)\n                countZeroes2++;\n            sum2 += (long)num;\n        }\n        \n        if(countZeroes1==countZeroes2 && sum1==sum2){\n            return sum1 + countZeroes1;\n        }\n\t\t// if any of count is zero then check if possible to construct\n        if(countZeroes1==0 || countZeroes2==0){\n            if((sum1<sum2 && countZeroes1==0) || (sum2<sum1 && countZeroes2==0))\n                return -1; //array with less sum is having no zeroes, so not possible\n            long diff = Math.abs(sum1-sum2); // get diff of sum\n            if(diff < Math.max(countZeroes2, countZeroes1)) \n                return -1; // if array that has c zeroes would require at least c 1\'s for sum to be equal. \n\t\t\t\t//but if the diff is less than the 1\'s required that means it is not possible to just add 1\'s in this array\n        }\n\t\t// now simply do binary search\n        if(sum1 >= sum2){\n            return getSum(sum1, countZeroes1, sum2, countZeroes2);\n        }\n        return getSum(sum2, countZeroes2, sum1, countZeroes1);\n    }\n    \n    private long getSum(long s1, int c1, long s2, int c2){\n        \n\t\t// for array with larger sum we will try to see all values possible\n\t\t// now if array with larger sum has c zeroes\n\t\t// then the min sum that it can have is sum + c so left starts with c and the maximun can be sum + 10^5\n\t\t// we will check if this sum is possible with other array\n\t\t// if not then simply proceed\n\t\t\n        int left = c1;\n        int right = 100000;\n        \n        while(left<right){\n            int mid = left + (right-left)/2;\n            long sum = s1 + mid; // get the new sum\n            long req = sum - s2; // check the extra sum required for array 2\n            if(req >= c2){ // if the req is greater than number of zeroes it is possible\n                right = mid;\n            }\n\t\t\t// else do left = mid+1\n            else{\n                left = mid+1;\n            }\n        }\n        return s1 + right;\n    }\n    \n}\n
['Binary Tree']