Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A shopkeeper sells n items where the price of theith item is price [ i ] . To maintain balance, theshopkeeper wishes to adjust the

A shopkeeper sells n items where the price of theith item is price[i]. To maintain balance, theshopkeeper wishes to adjust the price of itemssuch that the mediag of prices is exactly k. In onemove, the shopkeeper can increase or decrease theprice of any item by 1, and the shopkeeper canperform this move any number of timesV2C2627282930313233Find the minimum number of moves in which themedian of prices becomes exactly kNote: The index of the median of an array ofm sorted elements, where m.is odd, is (m=1)/2. Forexample, [2,5,4,1,1,1,6] sorted is [1,1,1,2,4,561. lts length is 7 so the median is at index (7+1)/24 using 1-based indexing. The median is 2.ExampleConsider n =5, price =[4,2,1,4,71and k=3.Decrease price[0] by 1, the resulting array is [3,2,1,4,7], on sorting, this becomes /1,2,3,477, whosemedian equals k -3, Thus, in one move, themedian becomes 3 and the answer is 1.

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

More Books

Students also viewed these Databases questions

Question

Why is a C corporation a separate entity?

Answered: 1 week ago

Question

7. Where Do We Begin?

Answered: 1 week ago

Question

a. What is the purpose of the team?

Answered: 1 week ago