Find optimal weights which can be used to weigh all the weights in the range [ 1 , X ] | Function to find the optimal weights ; Number of weights required ; Finding the value of required powers of 3 ; Optimal Weights are powers of 3 ; Driver code
function findWeights ( X ) { let sum = 0 ; let power = 0 ; let number = 3 ; while ( sum < X ) { sum = number - 1 ; sum = Math . floor ( sum / 2 ) ; power ++ ; number *= 3 ; } let ans = 1 ; for ( let i = 1 ; i <= power ; i ++ ) { document . write ( ans + " " ) ; ans = ans * 3 ; } } let X = 2 ; findWeights ( X ) ;