Related Articles
Find optimal weights which can be used to weigh all the weights in the range [1, X]
• Last Updated : 28 Jun, 2019

Given an integer X, the task is to find an optimal set of weights {w1, w2, w3, …, wn} 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

WeightsLeft SideRight Side
111
22 + 13
333
441 + 3
55 + 1 + 39
66 + 39
77 + 31 + 9

Input: X = 20
Output: 1 3 9 27

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

1. One optimal approach is to use weights which are a powers of 3 i.e. {1, 3, 9, 27, 81, 243, …}
2. 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.
3. 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 ``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;``}`

## 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. `

## 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`

## 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.`
Output:
```1 3
```

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up