This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

A problem regarding a java question

 
Ranch Hand
Posts: 32
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 76825
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
If you want to look young and thin, hang around old, fat people. Or this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic