Given two arrays of integers, B and A, how can we rearrange their elements such that ∏ A[i]B[i] for all i is maximized?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If the arrays contain non-negative numbers, you just sort both A and B in descending order. To see that this gives the maximum product, consider that once A and B are sorted in this order, you can try to swap two items of A, say A[i] and A[j] s.t. i
i.e. A[i]B[j]-B[i] A[j]B[i]-B[j], which equals (A[i]/A[j])(B[j]-B[i]) where the exponent is zero or negative because B[i] ≥ B[j]. By assumption A[i] ≥ A[j] so A[i]/A[j] ≥ 1. Therefore the ratio is 1 or less as the exponent is either 0 or negative. This shows that the new product is smaller in value than the old product. Note: this is just an illustration, not a formal proof because it considers only the swapping of two elements.