UBER Coding Question – Solved

10 Live
Sum of Arrays Given two arrays each of length n, arr1 and arr2, in one operation, any two elements of an array can be swapped. This can occur any number of times. Find the maximum possible sum of i * (arr2[i] - arr1[i]) for all 1 ≀ i ≀ n after rearranging the arrays. Since the maximum possible sum can be large, return the value modulo (10^9 + 7). Example n = 4 arr1 = [2, 1, 3, 4] arr2 = [2, 3, 2, 3] Some of the possible rearrangements: arr1: [2, 1, 3, 4], arr2: [2, 3, 2, 3] β†’ sum = 0 + 4 + 0 - 4 = 0 arr1: [3, 1, 2, 4], arr2: [2, 3, 2, 3] β†’ sum = -1 + 4 + 0 - 4 = -1 arr1: [3, 1, 2, 4], arr2: [3, 2, 2, 3] β†’ sum = 0 + 2 + 0 - 4 = -2 The maximum sum possible is 7, and 7 modulo (10^9 + 7) = 7. Function Description Complete the function getMaxSumOfArray in the editor below. getMaxSumOfArray has the following parameter(s): - int arr1[n]: an array of integers - int arr2[n]: an array of integers Returns - int: the maximum possible sum, modulo (10^9 + 7) Constraints - 1 ≀ n ≀ 10^5 - 1 ≀ arr1[i] ≀ 10^9 - 1 ≀ arr2[i] ≀ 10^9 Input Format for Custom Testing Sample Case 0 Sample Input arr1[] size n = 3 arr1 = [1, 2, 3] arr2[] size n = 3 arr2 = [10, 10, 10] Sample Output 50 Explanation Given n = 3, arr1 = [1, 2, 3] and arr2 = [10, 10, 10]. If arr1 is changed to [3, 2, 1] and arr2 remains unchanged, the value of i * (arr2[i] - arr1[i]) = [7, 16, 27], and the total sum is 50. Sample Case 1 Sample Input arr1[] size n = 4 arr1 = [3, 1, 2, 6] arr2[] size n = 4 arr2 = [2, 8, 4, 9] Sample Output 48 Explanation Original calculation: i * (arr2[i] - arr1[i]) = [-1, 14, 6, 12] β†’ sum = 31 Rearranging arr1 to [6, 1, 2, 3] β†’ new sum = 40 Rearranging arr1 to [6, 3, 2, 1] and arr2 to [2, 4, 8, 9] β†’ i * (arr2[i] - arr1[i]) = [-4, 2, 18, 32] β†’ sum = 48

Asked in: UBER

Image of the Question

Question Image Question Image Question Image Question Image

All Testcases Passed βœ”



Passcode Image

Solution


Please login to view the solution


Related Questions

| You are given a board of size M Γ— N where each cell can be either empty ('O') o… |
| Undirected Coloured Graph Shortest Path You are given an undirected weight… |
| Village Voyage A computer game "Village Voyage" has N villages (labeled 1 to… |
| Academic Decathlon Students are being selected for an academic decathlon tea… |
| Count Swaps During Custom Sorting Analyze the efficiency of the following so… |
| Stacey is coordinating a beach clean-up event with her university's Women in ST… |