id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

jebbert@volusia.k12.fl.us > test2021_02_11_HL_mass
prev  |  next  |  chance

Write a method that returns the minimum number of integer masses needed to find the mass of an object with integer mass using a balance scale. The objects being massed will have an integer mass between 1 and 'upto', inclusive. For example, if 'upto' is 5, the mass of the object could be 1, 2, 3, 4, or 5. If 'bothSides' is false, you must place the object to be massed on one side of the scale, and all the other masses you are using on the other side of the scale. However, if 'bothSides' is true, you may place any mass on any side. Here is an example: Suppose you are trying to find the mass of an object that is know to be 'upto' 5 kg and 'bothSides' is false. In this case, you could use masses of 1, 2, and 3 kg or maybe use 1, 2, and 4 kg, but you could NOT use 1, 3, and 4 kg because then you could not find the mass if the mass was 2 (you MUST make the scale balance to "find" the mass). Either way, the fewest number of masses you would need is 3 (not counting the mass you are trying to find). In that same situation, if 'bothSides' is true, you could now place your measuring masses on either side of the scale. This helps, in general, but not in this case. We would still need three test masses. However, as the 'upto' value increases, there WOULD be a benefit to being able to place the test masses on either side. In fact, if 'upto' was 4, and 'bothSides' was false, you would need three test masses. However, if 'upto' was 4, and 'bothSides' was true, you would only need two test masses. This is because we could choose 1 kg and 3 kg as our masses. We could find the mass of the object if it is 1, 3, or 4 kg by placing the masses all on one side. But we could also find the mass of the object if it is 2 kg by placing the 1 kg mass with the object and the 3 kg mass on the other side.


test2021_02_11_HL_mass(9241, true) → 9
test2021_02_11_HL_mass(45, true) → 5
test2021_02_11_HL_mass(119, true) → 5

...Save, Compile, Run (ctrl-enter)

public int test2021_02_11_HL_mass(int upto, boolean bothSides) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 280

Copyright Nick Parlante 2017 - privacy