programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Devaka Cooray
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Jeanne Boyarsky
• Tim Cooke
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Tim Moores
• Mikalai Zaikin
• Carey Brown
Bartenders:

# A problem regarding a java question

Ranch Hand
Posts: 32
• Number of slices to send:
Optional 'thank-you' note:
Question:

Problem : A room contains furniture of different weights. The furniture should be arranged from
least weight to the highest from the door of the room. Only swapping is allowed. The cost of
swapping is the sum of their weights. The swapping should be made such that the cost is least.

Sample Input #1 noOfFurnitures = 3 int[]{5,4,2}
Here the weights of the furniture are 5,4,2 units respectively where the index position is the
position of the furniture from the door.

Here the furniture nearest to the door is of weight 5

So swap position 1 and 3 to get {2,4,5} cost = 5+2=7
which is the desired result.

Sample Input #2 noOfFurnitures = 4 int[]{6,1,2,4}

swap position 2 and 3 {6,2,1,4} cost = 1+2 = 3
swap position 3 and 4 {6,2,4,1} cost = 1+4 = 5
swap position 1 and 4 {1,2,4,6} cost = 6+1 = 7

Total cost = 3+5+7 = 15

The code should be contained in the package com.adp.apartment

and should have the method signature

public int getFurnitureMovement(int noOfFurnitures , int[] weight)

You may add as many other methods in your class

I am not sure whether I have implemented it correctly. Even if so, it is not working for duplicates as it not mentioned in the question whether or not the array contains a duplicate. Please suggest me a way to compute in the situation of duplicates.

Marshal
Posts: 78682
374
• Number of slices to send:
Optional 'thank-you' note:
I think you need to write down on a piece of paper what you plan to do in those different situations.

 Whatever. Here's a tiny ad: a bit of art, as a gift, that will fit in a stocking https://gardener-gift.com
reply
Similar Threads