Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8584925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:53:23+00:00 2026-06-11T21:53:23+00:00

This is a question from some programming contest( i don’t know exactly which programming

  • 0

This is a question from some programming contest( i don’t know exactly which programming contest it belongs to , i saw it a year ago ). The question is as follows:

There are N buildings, each having its own height ( not necessarily unique )

{h1,h2,h3,...,hn}

We have to make all the buildings of same height say h.

The operations allowed are:

  1. We can add some height to a building.
  2. We can remove some height from a building.

There is a cost associated with each building to remove/add a unit height. Say c(i) be the cost to remove/add a unit height to a building. The respective cost are as follows:

{c1,c2,c3,...,cn}

Assuming that we have enough height(unit) available, we have to find the min cost which is required to make all buildings of the same height.

Input:
First Line will specify N the number of buildings. ( 1<=N<=100000).
Second Line of Input will be for the height of buildings.

{h1,h2,h3,...,hn}

Third line of input will give the cost array

 {c1,c2,c3.....,cn}

Output

The output will be simply the min cost required.

Sample Input:

3

2 1 3

10 1000 1

Sample Output

12

Explanation: Make all the buildings of height 1, so the cost will be
10*(2-1) + 1000*(1-1) + 1*(3-1) i.e 12.

I know the brute force force method which is O(n^2).

The brute force method i thought is as follows:

Whatever the common height h be, it must be from the

 {h1,h2,h3,....,hn}

i.e. h must be equal to any of h(i) .
Now checking for each h(i) I can compute the answer in O(n^2).

is it possible to do it faster?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T21:53:24+00:00Added an answer on June 11, 2026 at 9:53 pm

    O(n log(n)) Solution:

    Let h(i) denote height of the ith building and let c(i) denote the cost of the ith building.

    1. Step-1: Sort the building according to the heights of the respective buildings in decreasing order. Let this arrangement be called P. i.e. P(1) is the height of the largest building and P(n) is the height of the smallest building. This takes O( n log n).
    2. Step-2: Sum all the heights of the building. Let S denote this sum. This step takes O(n) time.
    3. Step-3: Let Cost_of_Increase(i) denote the Cost if we make heights of all the buildings that have heights LESS than the ith building equal to the height of the ith building in the SORTED ARRAY P, i.e. Cost_of_Increase(i) if we make the buildings P(i-1), P(i-2), … P(n) equal to the height of the P(i) th building.

    Now use this recursion:

    Cost_of_Increase(i) = Cost_of_Increase(i-1) + ( h(i)-h(i-1) )*( Sum of costs of P(i-1) th building to P(n) th builing )

    Note that in the above recursion the order of i and i-1 is according to the order of P, i.e. the sorted order.

    Now let Cost_of_decrease(i) denote the cost if we make all the buildings that have heights GREATER than the ith building equal to hte height of the ith building in the SORTED ARRAY P, i.e. Cost_of_decrease(i) if we make the buildings P(1), P(2), … P(i-2), P(i-1) equal to the height of the P(i) th building.

    For this use this recursion:

    Cost_of_decrease(i) = Cost_of_decrease(i+1) + ( h(i+1)-h(i) )*( Sum of cost of P(1) th building to P(i-1) th building )

    So total cost for making the height of all the buildings equal to P(i) th building is:

    Cost_of_Increase(i) + Cost_of_decrease(i).

    Once we have this for all the buildings, just check for which one the cost is smallest, and that is the answer.

    Hope it helps !

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is similar to (but different from) this question . Here is some simple
This question came about as a result of some mixed-language programming. I had a
This question goes beoynd just programming, but I'd like to get some input on
A couple of questions re static classes. Some of this is from stuff I
This is a continuation of this question from yesterday . Here are my three
I got this question from my cousin: What will be the best way to
Unfortunately I am not writing this question from my Developing PC so I might
This question results from hours of googling highstocks, zoom, extremes, ranges, and every other
This question stems from Hartl's Rails Tutorial (progressed in chapter 9) - sorry if
This question arose from the discussion in the comments of this answer . First,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.