Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A shopkeeperelis nitems where the price of the thitem is pricelmaintain balance, the shopkeeperwishes to adjust the price of items such that the median of

A shopkeeperelis nitems where the price of the thitem is pricelmaintain balance, the shopkeeperwishes to adjust the price of items such that the median of prices is exactly k. In one move, the shopkeeper can increase or decrease the price of any item by 1, and the shopkeeper can perform this move any number of times.Find the minimum number of moves in which the median of prices becomes exactly k.Note: The index of the median of an array of m sorted elements, where m is odd, is (m+1)/2. For example, (2,5,4,1,1,1,6) sorted is [1,1,1,2,4,5,6]. Its length is 7 so the median is at index (7+1)/2=4 using 1-based indexing. The median is 2.ExampleConsider n =5, price =[4,2,1,4,77 and k =3,Decrease price[0] by 1, the resulting array is (3,2,1,4,7); on sorting, this becomes (1,2,3,4,71, whosemedian equals k =3. Thus, in one move, the medianbecomes 3 and the answer is 1.Function DescriptionComplete the function getMinimumMoves in the editor below

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

Fundamentals Of Database Systems

Authors: Ramez Elmasri, Shamkant B. Navathe

7th Edition Global Edition

1292097612, 978-1292097619

More Books

Students also viewed these Databases questions

Question

Which personal relationships influenced you the most?

Answered: 1 week ago

Question

What were your most important educational experiences?

Answered: 1 week ago