• Post Reply Bookmark Topic Watch Topic
  • New Topic

A problem regarding a java question  RSS feed

 
kumarjit banerjee
Ranch Hand
Posts: 32
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you need to write down on a piece of paper what you plan to do in those different situations.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!