Finding a Hidden Benchmark Value
Updated
Problem statement
You are given an array A with N numbers, A = [a0, a1, ..., aN-1], and an array B with M numbers, B = [b0, b1, ..., bM-1].
Choose a benchmark value D, a whole number with D ≥ 0. Every element then gets a score: an element that is at most D scores 1 point, and an element greater than D scores 2 points. SA is the total score of the elements of A, and SB is the total score of the elements of B.
For example, if A = [6, 10, 9, 7, 8], B = [4, 1, 5, 3, 2] and D = 5, every element of A is greater than 5, so SA = 5 × 2 = 10. Every element of B is at most 5, so SB = 5 × 1 = 5.
Find the D that makes SA - SB as large as possible. Caution: this is SA - SB itself, not its absolute value, so the best difference can be negative. If more than one D gives the largest SA - SB, return the smallest one.
Complete the function findHiddenBenchmarkValue in the editor.
findHiddenBenchmarkValue has the following parameters:
int[] A: the first arrayint[] B: the second array
The function returns an int: the smallest D ≥ 0 that maximizes SA - SB.
Function
findHiddenBenchmarkValue(A: int[], B: int[]) → intExamples
Example 1
A = [6, 10, 9, 7, 8]B = [4, 1, 5, 3, 2]return = 5When D is 5, the score of A and B are 10 and 5, respectively. Then, the value of SA-SB is 5 which is also maximum.
Example 2
A = [3, 2, 1]B = [5, 6]return = 0When D is 0, the scores of A and B are 6 and 4 respectively. Then, the value of SA-SB is 2 which is also maximum.
Example 3
A = [10, 6, 3]B = [3, 3, 10, 3, 2, 11]return = 3When D is 3, the scores of A and B are 5 and 8 respectively. Then, the value of SA-SB is -3 which is also maximum. Although the maximum value of SA-SB is -3 when D is 11 or 12 as the score of A and B are 3 and 6, respectively, the answer is 3 because 3 is smaller than 11.
Example 4
A = [1, 2]B = [5, 6, 7, 8]return = 8When D is 8, the scores of A and B are 2 and 4 respectively. Then, the value of SA-SB is -2 which is also maximum. Remember that the difference between SA and SB is not an absolute value.
Constraints
1 ≤ N, M ≤ 105, whereN = A.lengthandM = B.length1 ≤ ai ≤ 1081 ≤ bj ≤ 108
More Samsung interview questions
Similar questions at other companies
- Hard Disk Clustering CostConfluent · OAConfluent interview questions
- Maximize K-Element AND with an Increment BudgetAmazon · OA
- Delete and Freeze GameRubrik · OARubrik interview questions
- Most Segments That Sort the ArrayRippling · OARippling interview questions
- Most Items Within Budget from a Start IndexHudson River Trading · OAHudson River Trading interview questions
- Luna and the Colorful SocksPhonePe · OAPhonePe interview questions
- Minimum Starting Coins to Buy All ProductsMicrosoft · OAMicrosoft interview questions
- Minimum Swaps to Sort a Ternary Array After UpdatesGoogle · OAGoogle interview questions
- Disk ClusteringAirbnb · OAAirbnb interview questions
- Get Max EfficiencySalesforce · OASalesforce interview questions