Wednesday, October 3, 2012

Weighing Puzzle

Problem Statement:

You are given n(<=8) weights, w1,w2, · · · ,wn. Find all distinct weight measurements that you can make using a common balance and the given weights. For example, assume that you are given weights of 1, 3, and 5 units. You can weigh all measures from 1 to 9 units as:

•1=1 2=3−1
•3=3 4=3+1
•5=5 6=5+1
•7=5+3−1 8=5+3
•9=5+3+1

You can subtract a specific weight you have, by adding it along with item to be measured.
Your input will contain the number of weights you have, followed by the weights themselves.
You need to output all the possible weights you can measure in the increasing order.
You should not repeat any measures.

Input: First Line contains N(number of distinct weights)(N<=8). Next line will have N distinct weights separated by space.
 
Output: list of distinct weights possible in increasing order followed by ‘\n’.

Code in C++:


No comments:

Post a Comment