Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Equal Price A shop in HackerLand contains n items where the price of the th item is price ( l ] , In one operation,

Equal Price
A shop in HackerLand contains n items where the price of the th item is price(l], In one operation, the price of any one item can be increased or decreased by 1.
Given q queries denoted by the array queryl, find the minimum number of operations required to make the price of all items equal to each queryll](0<=/< q).
Note: All queries are independent of each other, i.e., the original price of items is restored after the completion of each query.
Example:
Consider n=3, q=4, pricel]=[1,2,3), queryD =[3,2,1,5]
query[0]=3 The number of operations required =[2,1,0] tomake the price of all elements equal to 3. Total number ofoperations =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].
Function Description
Complete the function countMinimumOperations in the editor below.
countMinimumOperations has the following parameters:
int price[n]: the original prices of each item int querylq]: the queries
Returns
long_int[q]: the answers to the queries in their order of input
Constraints
15n52*105
15 pricell]<=10\deg
15 q<=2*105
1<= queryll]<=10\deg

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

How To Build A Million Dollar Database

Authors: Michelle Bergquist

1st Edition

0615246842, 978-0615246840

More Books

Students also viewed these Databases questions

Question

Persuasive Speaking Organizing Patterns in Persuasive Speaking?

Answered: 1 week ago