Minimum Equal Sum of Two Arrays After Replacing Zeros
Intuition\nThe code aims to find the minimum equal sum of two arrays after replacing zeros in the arrays.\n Describe your first thoughts on how to solve this pr
# Intuition\nThe code aims to find the minimum equal sum of two arrays after replacing zeros in the arrays.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Initialize variables s1 and s2 to keep track of the sums of nums1 and nums2, respectively.\n2. Initialize variables z1 and z2 to count the number of zeros in nums1 and nums2, respectively.\n3. Iterate through nums1 and nums2 to calculate their sums and count the number of zeros.\n4. Check if s1 is equal to s2. If they are equal, return s1 since it\'s already the minimum equal sum.\n5. If s1 is less than s2, check if there are zeros in nums1 (i.e., z1 > 0). If there are zeros, return s2 as the minimum equal sum; otherwise, return -1.\n6. If s2 is less than s1, check if there are zeros in nums2 (i.e., z2 > 0). If there are zeros, return s1 as the minimum equal sum; otherwise, return -1.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n- $$O(n+m)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n- $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```Javascript []\n/**\n * @param {number[]} nums1\n * @param {number[]} nums2\n * @return {number}\n */\nvar minSum = function(nums1, nums2) {\n    let n=nums1.length;\n    let m=nums2.length;\n    \n    let s1=0,s2=0;\n    let z1=0,z2=0;\n    \n    for(let i=0;i<n;i++){\n        s1+=nums1[i];\n        if(nums1[i]===0){\n            z1++;\n            s1++;\n        }\n    }\n    \n    for(let i=0;i<m;i++){\n        s2+=nums2[i];\n        if(nums2[i]===0){\n            z2++;\n            s2++;\n        }\n    }\n    \n    if(s1===s2)\n        return s1;\n    else{\n        if(s1<s2){\n            if(z1>0)\n                return s2;\n            else\n                return -1;\n        }\n        else{\n            if(z2>0)\n                return s1;\n            else\n                return -1;\n        }\n    }\n    return -1;  \n    \n};\n```\n```C++ []\nclass Solution {\npublic:\n    #define loop(n) for(int i=0;i<n;i++)\n    using ll=long long;\n    \n    long long minSum(vector<int>& nums1, vector<int>& nums2) {\n        int n=nums1.size();\n        int m=nums2.size();\n        \n        ll sum1=0,sum2=0;\n        int z1=0,z2=0;\n        \n        loop(n){\n            sum1+=nums1[i]    ;\n            if(nums1[i]==0){\n                sum1++;\n                z1++;\n            }\n        }\n\n        loop(m){\n            sum2+=nums2[i]    ;\n            if(nums2[i]==0){\n                sum2++;\n                z2++;\n            }\n        }\n\n        if(sum1==sum2)\n            return sum2;\n        else{\n            if(sum1<sum2){\n                if(z1>0)\n                    return sum2;\n                else\n                    return -1;\n            }else{\n                if(z2>0)\n                    return sum1;\n                else\n                    return -1;\n            }\n        }\n        return -1;\n    }\n};\n```\n\n\n
['Counting', 'Iterator', 'C++', 'JavaScript']