You are not logged in. Please login at to post your questions!


AppPerfect question

The following question was asked in our campus for AppPerfect company. If possible someone can suggest a good approach

There are n shops of a certain product of given quantity on the x-axis. Each shop either buys the product, represented by +ve value or sells the product, represented by -ve value. You are initially at x = 0 and, our aim to travel minimum distance and satisfy the demands of all the shops who want to buy the product.

Input : the first contains n, number of shops presents the second line contains n integers containing the location of shos of the ith shop the third line contains n integers containing the quantity of product it wants to buy or sell

Output : A single integer which will be minimum distance by us to satisfy every demand

Sample: input: 3

-10 1 2

5 -6 1

output: 14

Explanation: we first go to from 0 to 1 for purchase and then to 2 for selling and then to -10 to sell again

Contraints - 1<= n <= 1e3, -1e5 <= cooridnates <= 1e5 -1e5 <= demand <= 1e5

asked 17 Oct '18, 12:47

sonu_628's gravatar image

accept rate: 8%

edited 21 Oct '18, 15:32

Could you please explain the sample test case. it is quite unclear


answered 17 Oct '18, 13:40

vipin1407's gravatar image

accept rate: 13%

The sum of all values of demand array is zero always,right?


answered 17 Oct '18, 13:53

ram_24's gravatar image

accept rate: 37%

not necessary

(21 Oct '18, 15:33) sonu_6283★
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 17 Oct '18, 12:47

question was seen: 136 times

last updated: 21 Oct '18, 15:33