My Blog SCJP 5 SCWCD 5
Vinod Tiwari | Twitter | Shikshanirman | Guftgu
SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Riley Thomas wrote:1: Weigh 6 balls on each side. The heavier side (obviously) contains the irregular ball.
Riley Thomas wrote:Assuming there is no limit to the amount of balls that can be placed on the scale, the answer is 3 weighs.
1: Weigh 6 balls on each side. The heavier side (obviously) contains the irregular ball.
2: Of those 6, weight 3 on each side. Again, the heavier side has the irregular ball.
3: Of those 3, weigh any 2. If they're equal, the third ball is the irregular.
Subhash Kumar
Attitude is everything
where 'n' is the no. of balls.log n to the base 2
Coderanch, best ever forum on the net. Hands down.
Coderanch, best ever forum on the net. Hands down.
Orton K Randy wrote:If the left is heavier than the right, the conclusion is that the culprit is in the set we placed in the left tray(assuming we're looking for 1 ball that's heavier than the rest).
Mike Simmons wrote:
Orton K Randy wrote:If the left is heavier than the right, the conclusion is that the culprit is in the set we placed in the left tray(assuming we're looking for 1 ball that's heavier than the rest).
That particular conclusion only makes sense if we know, in advance, that the ball we're looking for is heavier than the rest. You're making an assumption not present in the problem statement - one I've pointed out previously in this thread.
Coderanch, best ever forum on the net. Hands down.
Deepak Lal wrote:which is the correct answer?
SCJP
Visit my download page
Randall Twede wrote:i fail to see what it matters how much different the ball weighs..
Roger Rammer wrote:Superb! great minds think alike ;-)
Riley Thomas wrote:Assuming there is no limit to the amount of balls that can be placed on the scale, the answer is 3 weighs.
1: Weigh 6 balls on each side. The heavier side (obviously) contains the irregular ball.
2: Of those 6, weight 3 on each side. Again, the heavier side has the irregular ball.
3: Of those 3, weigh any 2. If they're equal, the third ball is the irregular.
Orton K Randy wrote:I was asked the same question in my interview months ago. I am surprised no one in here mentioned the log/exponentiation? After trying out for several no. of balls, you'd have noticed a pattern. Ya don't even think twice. Min no. of weighings(in a physical balance of course) required is
where 'n' is the no. of balls.log n to the base 2
So if the no. of balls is 4, you need 2 weighings, for 8 you need 3, for 16 you need 4 and so on. Note that for 12 you need 3 and NOT 4. Likewise for 5,6,7 you need only 2 and NOT 3. So in programming terms, it's floor(log n to the base 2).
You might well be aware of this, still this one's for those trying to work out everytime.
Consider Paul's rocket mass heater. |