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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# A problem regarding a java question

Ranch Hand
Posts: 32
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: 59146
180
I think you need to write down on a piece of paper what you plan to do in those different situations.

 It is difficult to free fools from the chains they revere - Voltaire. tiny ad: Rocket Oven Kickstarter - from the trailboss https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss