Given an integer **X**, the task is to find an optimal set of weights **{w _{1}, w_{2}, w_{3}, …, w_{n}}** such that we can weigh/determine all the weights from

**1**to

**X**using a two-sided weighing balance pan.

**Note**that all the weights must be unique and

**n**should be as minimum as possible.

**Examples:**

Input:X = 7

Output:1 3 9

Weights Left Side Right Side 1 1 12 2 + 133 3 34 4 1+35 5 + 1+396 6 + 397 7 + 31+9

Input:X = 20

Output:1 3 9 27

**Approach:**

- One optimal approach is to use weights which are a powers of 3 i.e.
**{1, 3, 9, 27, 81, 243, …}** - It can be proved through induction that using
**{1, 3, 9, 27, 81, …, pow(3, n)}**, we can balance all the weights from**1**to**(pow(3, n + 1) – 1) / 2**. - So, find the values of
**n**such that all the values from**1**to**X**can be balanced and print the results.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the optimal weights ` `void` `findWeights(` `int` `X) ` `{ ` ` ` `int` `sum = 0; ` ` ` ` ` `// Number of weights required ` ` ` `int` `power = 0; ` ` ` ` ` `// Finding the value of required powers of 3 ` ` ` `while` `(sum < X) { ` ` ` `sum = ` `pow` `(3, power + 1) - 1; ` ` ` `sum /= 2; ` ` ` `power++; ` ` ` `} ` ` ` ` ` `// Optimal Weights are powers of 3 ` ` ` `int` `ans = 1; ` ` ` `for` `(` `int` `i = 1; i <= power; i++) { ` ` ` `cout << ans << ` `" "` `; ` ` ` `ans = ans * 3; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `X = 2; ` ` ` ` ` `findWeights(X); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `public` `class` `GFG ` `{ ` ` ` ` ` `// Function to find the optimal weights ` ` ` `static` `void` `findWeights(` `int` `X) ` ` ` `{ ` ` ` `int` `sum = ` `0` `; ` ` ` ` ` `// Number of weights required ` ` ` `int` `power = ` `0` `; ` ` ` `int` `number = ` `3` `; ` ` ` ` ` `// Finding the value of required powers of 3 ` ` ` `while` `(sum < X) ` ` ` `{ ` ` ` `sum = number - ` `1` `; ` ` ` `sum /= ` `2` `; ` ` ` `power++; ` ` ` `number *= ` `3` `; ` ` ` `} ` ` ` ` ` `// Optimal Weights are powers of 3 ` ` ` `int` `ans = ` `1` `; ` ` ` `for` `(` `int` `i = ` `1` `; i <= power; i++) ` ` ` `{ ` ` ` `System.out.print(ans + ` `" "` `); ` ` ` `ans = ans * ` `3` `; ` ` ` `} ` ` ` `} ` ` ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `X = ` `2` `; ` ` ` ` ` `findWeights(X); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by Sam007. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to find the optimal weights ` `def` `findWeights(X): ` ` ` ` ` `sum` `=` `0` ` ` ` ` `# Number of weights required ` ` ` `power ` `=` `0` ` ` ` ` `# Finding the value of required powers of 3 ` ` ` `while` `(` `sum` `< X): ` ` ` `sum` `=` `pow` `(` `3` `, power ` `+` `1` `) ` `-` `1` ` ` `sum` `/` `/` `=` `2` ` ` `power ` `+` `=` `1` ` ` ` ` `# Optimal Weights are powers of 3 ` ` ` `ans ` `=` `1` ` ` `for` `i ` `in` `range` `(` `1` `, power ` `+` `1` `): ` ` ` `print` `(ans, end ` `=` `" "` `) ` ` ` `ans ` `=` `ans ` `*` `3` ` ` `# Driver code ` `X ` `=` `2` ` ` `findWeights(X) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to find the optimal weights ` ` ` `static` `void` `findWeights(` `int` `X) ` ` ` `{ ` ` ` `int` `sum = 0; ` ` ` ` ` `// Number of weights required ` ` ` `int` `power = 0; ` ` ` `int` `number = 3; ` ` ` ` ` `// Finding the value of required powers of 3 ` ` ` `while` `(sum < X) ` ` ` `{ ` ` ` `sum = number - 1; ` ` ` `sum /= 2; ` ` ` `power++; ` ` ` `number *= 3; ` ` ` `} ` ` ` ` ` `// Optimal Weights are powers of 3 ` ` ` `int` `ans = 1; ` ` ` `for` `(` `int` `i = 1; i <= power; i++) ` ` ` `{ ` ` ` `Console.Write(ans + ` `" "` `); ` ` ` `ans = ans * 3; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `public` `void` `Main () ` ` ` `{ ` ` ` `int` `X = 2; ` ` ` `findWeights(X); ` ` ` `} ` `} ` ` ` `// This code is contributed by ajit. ` |

*chevron_right*

*filter_none*

**Output:**

1 3

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Maximum number of distinct positive integers that can be used to represent N
- Check if given coins can be used to pay a value of S
- Balance pans using given weights that are powers of a number
- Shortest path from source to destination such that edge weights along path are alternatively increasing and decreasing
- Find all the possible numbers in a range that can be evenly divided by its digits
- Optimal Strategy for a Game | DP-31
- Secretary Problem (A Optimal Stopping Problem)
- Optimal strategy for a Game with modifications
- Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M
- Greatest divisor which divides all natural number in range [L, R]
- Sum of all numbers in the given range which are divisible by M
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors
- Find the prime numbers which can written as sum of most consecutive primes
- Find the number of rectangles of size 2*1 which can be placed inside a rectangle of size n*m
- Find the sums for which an array can be divided into sub-arrays of equal sum
- Find the lexicographically smallest sequence which can be formed by re-arranging elements of second array
- Find the smallest positive number which can not be represented by given digits
- Find number of square of area Z which can be built in a matrix having blocked regions
- Find the maximum angle at which we can tilt the bottle without spilling any water
- C++ program to find all numbers less than n, which are palindromic in base 10 and base 2.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.