Skip to content

2918. Minimum Equal Sum of Two Arrays After Replacing Zeros #1666

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to replace all zeros in two given arrays with strictly positive integers such that the sums of both arrays become equal, and we need to find the minimum possible equal sum. If it's impossible, we return -1.

Approach

  1. Calculate Sums and Zeros: For each array, compute the sum of non-zero elements and count the number of zeros.
  2. Determine Minimum Possible Sums: For each array, the minimum possible sum after replacing zeros with 1s (smallest positive integer) is calculated. This is done by adding the sum of non-zero elements to the count of zeros (since each zero is replaced by 1).
  3. Check Conditions:
    • If both arrays have zeros, the minimum possible equal sum is the maximum of their mini…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@kovatz
Comment options

kovatz May 10, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim May 10, 2025
Maintainer Author

Answer selected by kovatz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants