Monday, November 9, 2015

Minimum number of weights - PrismoSkills



Minimum number of weights - PrismoSkills
  • Choose a minimum number of weights to be able to measure all weights between 1-40 kg.
------------------------------------------------------------------------------------
Soln: To be able to measure 1 kg, one weight of 1 kg is a must.
To be able to measure 40 kg, sum of all weights chosen must be 40 kg.
Better Solution: The above solution does not take into account that the weights can be used as negative weights also. So, the weights need not be kept only on side of the balance, they can be kept on the other side of the balance too to increase the weight of the object.

1 kg weight is still a must.
To measure 2 kg, we can either have 2 kg or 1 kg +1 kg or 3 kg -1 kg weight combinations.
Off course, 3-1 is better, because we can then measure, 1, 2, 3 and 4 kg weights.

To measure 5 kg, we can do it as
5 or 1+4 or (3-1)+3 or 3+2 or 6-1 or 7-(3-1) or 8-3 or 9-(3+1)
Best is to choose last combo so we will get all weights from 1 to 13.

So to measure 1 to 13 kg weights, we need 1, 3, and 9 kg weights.

To measure 14, we need x-13=14 --> x=27 kg weight.

This seems to imply that a series of 1,3,9,27 ... 3^r can measure all
weights between 1 and 1+3+9+27...3^r = (3^r-1)/2

Optimization: Now, this can be optimised by binary method.
If we have one weight of 20 kg and 20 weights of 1 kg, we can still measure all values.
=> number of weights required is 1 + 20 = 21

The 20 weights of 1 kg are used both above and below 20 kg and their range can again be halved.
We can have 1 weight of 20 kg as before and 1 weight of 10 kg and 10 weights of 1 kg.
=> no. of weights required = 1 + 1 + 10 = 12

Half again the number of 1 kg weights
=> no. of weights required = 1 + 1 + 1 + 5 = 8

Again half
=> no. of weights required = 1 + 1 + 1 + 1 + 2 = 6

Thus, 6 weights are needed having weights 20, 10, 5, 3, 1, 1

Now, let us generalize the solution:
If weight range is 1-N, then weights required are:
N/2, N/4, N/8 ... 1, 1 (This last one weight helps to reach 2k from k)
=> Number of weights required are: logN + 1
Read full article from Minimum number of weights - PrismoSkills

Labels

Review (572) System Design (334) System Design - Review (198) Java (189) Coding (75) Interview-System Design (65) Interview (63) Book Notes (59) Coding - Review (59) to-do (45) Linux (43) Knowledge (39) Interview-Java (35) Knowledge - Review (32) Database (31) Design Patterns (31) Big Data (29) Product Architecture (28) MultiThread (27) Soft Skills (27) Concurrency (26) Cracking Code Interview (26) Miscs (25) Distributed (24) OOD Design (24) Google (23) Career (22) Interview - Review (21) Java - Code (21) Operating System (21) Interview Q&A (20) System Design - Practice (20) Tips (19) Algorithm (17) Company - Facebook (17) Security (17) How to Ace Interview (16) Brain Teaser (14) Linux - Shell (14) Redis (14) Testing (14) Tools (14) Code Quality (13) Search (13) Spark (13) Spring (13) Company - LinkedIn (12) How to (12) Interview-Database (12) Interview-Operating System (12) Solr (12) Architecture Principles (11) Resource (10) Amazon (9) Cache (9) Git (9) Interview - MultiThread (9) Scalability (9) Trouble Shooting (9) Web Dev (9) Architecture Model (8) Better Programmer (8) Cassandra (8) Company - Uber (8) Java67 (8) Math (8) OO Design principles (8) SOLID (8) Design (7) Interview Corner (7) JVM (7) Java Basics (7) Kafka (7) Mac (7) Machine Learning (7) NoSQL (7) C++ (6) Chrome (6) File System (6) Highscalability (6) How to Better (6) Network (6) Restful (6) CareerCup (5) Code Review (5) Hash (5) How to Interview (5) JDK Source Code (5) JavaScript (5) Leetcode (5) Must Known (5) Python (5)

Popular Posts