A shop in HackerLand contains n
items where the price of the j
th item is price[j]
. In one operation, the price of any one item can be increased or decreased by 1.
Given q
queries denoted by the array query[]
, find the minimum number of operations required to make the price of all items equal to each query[i]
(0 ≤ i < q).
Note: All queries are independent of each other, i.e., the original price of items is restored after the completion of each query.
Function Description
Complete the function minimumOperationsRequired
in the editor.
minimumOperationsRequired
has the following parameters:
- 1.
int[] price
: an array of integers representing the prices of items - 2.
int[] query
: an array of integers representing the queries
Returns
int[]
: an array of integers where the i
th integer is the minimum number of operations required for the i
th query
Example 1:
Input: price = [1, 2, 3], query = [3, 2, 1, 5]
Output: [3, 2, 3, 9]
Explanation:Consider
n = 3
,q = 4
,price[] = [1, 2, 3]
,query[] = [3, 2, 1, 5]
- query[0] = 3. The number of operations required = [2, 1, 0] to make the price of all elements equal to 3. Total number of operations = 2 + 1 + 0 = 3.
- query[1] = 2, operations required = [1, 0, 1] 1 + 0 + 1 = 2
- query[2] = 1, operations required = [0, 1, 2] 0 + 1 + 2 = 3
- query[3] = 5, operations required = [4, 3, 2] 4 + 3 + 2 = 9
The answer is [3, 2, 3, 9].
🍓

input:
output: