Asked in: UBER
def factorial(n):
if n<=0:
return 0
return n*factorial(n-2)
// ... rest of solution available after purchase
```
To solve this problem, you first need to carefully analyze the expression you want to maximize:
Sum over i of i * (arr2[i] - arr1[i])
Here, i ranges from 1 to n, and you are allowed to reorder (permute) the elements of arr1 and arr2 independently any number of times to maximize this sum. The main goal is to find the maximum possible value of this sum after such rearrangements.
Step 1: Understand the Expression and What it Represents
The sum is composed of a weighted difference between elements of arr2 and arr1 at each index i, where the weight is i (the position in the array, starting from 1). Because you can rearrange arr1 and arr2 independently, you are basically free to pair elements of arr1 and arr2 in any order. The problem then becomes: find permutations of arr1 and arr2 that maximize
∑ i * (arr2[i] - arr1[i])
= ∑ (i * arr2[i]) - ∑ (i * arr1[i])
Given that you control both arr1 and arr2 orderings independently, your task is to maximize the difference between weighted sums of arr2 and arr1.
Step 2: Decompose the Problem
Notice that the sum is a difference of two sums weighted by the index i:
- Weighted sum of arr2: ∑ i * arr2[i]
- Weighted sum of arr1: ∑ i * arr1[i]
You want to maximize (weighted sum of arr2) - (weighted sum of arr1).
Step 3: How to Maximize Weighted Sums
Since the weights are simply i (1 through n), which are strictly increasing, the larger the element you place at a higher index, the more it contributes to the sum.
- To maximize ∑ i * arr2[i], you want to put the largest elements of arr2 at the largest indices.
- To minimize ∑ i * arr1[i], since this is subtracted, you want to put the largest elements of arr1 at the smallest indices, thereby minimizing the product i * arr1[i].
Step 4: Think of Sorting Strategy
Based on the above reasoning, for arr2 you want to sort the elements in ascending order and place the smallest at index 1, largest at index n. For arr1, you want to sort the elements in descending order, placing the largest elements at the smallest indices to minimize the weighted sum of arr1.
Why? Because:
- For arr2: Increasing order matches increasing indices, maximizing ∑ i * arr2[i].
- For arr1: Decreasing order matches increasing indices, which makes the sum ∑ i * arr1[i] smaller because larger elements get smaller weights.
Step 5: Formulate the Rearrangement
So the optimal rearrangement would be:
- Sort arr2 in ascending order
- Sort arr1 in descending order
Then compute the sum ∑ i * (arr2[i] - arr1[i]) with i from 1 to n.
Step 6: Verify Intuition with Examples
Try the provided examples:
- Example 1:
arr1 = [1, 2, 3] → sorted descending → [3, 2, 1]
arr2 = [10, 10, 10] → sorted ascending → [10, 10, 10] (same)
Then weighted sum arr2 = 10*1 + 10*2 + 10*3 = 60
Weighted sum arr1 = 3*1 + 2*2 + 1*3 = 3 + 4 + 3 = 10
Difference = 60 - 10 = 50, which matches the example.
- Example 2:
arr1 = [3, 1, 2, 6] → descending → [6, 3, 2, 1]
arr2 = [2, 8, 4, 9] → ascending → [2, 4, 8, 9]
Calculate weighted sums and difference, which matches the example’s optimal rearrangement.
Step 7: Handling Large Inputs and Modulo
Since the values and n can be large, the sum may exceed integer limits. To manage this:
- Use a data type capable of storing large sums (e.g., 64-bit integer)
- Take modulo (10^9 + 7) at each step after additions/multiplications to keep numbers manageable.
Step 8: Implementation Outline
- Sort arr1 descending
- Sort arr2 ascending
- Iterate i from 1 to n
- For each i, calculate i * (arr2[i] - arr1[i]) modulo (10^9 + 7) and add to result
- Return the final result
Step 9: Summary of Thought Process
- Recognize the sum as a difference of weighted sums
- Realize that maximizing difference means maximizing weighted sum of arr2 and minimizing weighted sum of arr1
- Use sorting strategies aligned with the monotonicity of weights to position elements optimally
- Carefully handle large sums with modular arithmetic
By following this approach, you efficiently and correctly determine the maximum possible sum after rearranging both arrays independently.
```