# Check if item can be measured using a scale and some weights

Given some weights of masses a^{0}, a^{1}, a^{2}, …, a^{100}, a being an integer, and a weighing scale where weights can be put on both the sides of the scale. Check whether a particular item of weight W can be measured using these weights and scale.

Constraints:2 ≤ W ≤ 10^{9}

**Examples:**

Input :a = 2, W = 5Output :YESExplanation :Weights of masses (2^{0}= 1) and (2^{2}= 4) can be placed on one side of the scale and the item can be placed on other side, i.e. 1 + 4 = 5

Input :a = 4, W = 11Output :YESExplanation :Weights of masses (4^{0}= 1) and (4^{1}= 4) and the item can be placed on one side and a weight of mass (4^{2}= 16) can be placed on the other side, i.e. 1 + 4 + 11 = 16

Input :a = 4, W = 7Output :NO

**Approach:**

^{9}

item or including current weight to the opposite side containing item or by not using that weight at all.

Below is the implementation of above approach .

## C++

`// CPP Program to check whether an item` `// can be measured using some weight and` `// a weighing scale.` `#include <bits/stdc++.h>` `using` `namespace` `std;` ` ` `// Variable to denote that answer has` `// been found` `int` `found = 0;` ` ` `void` `solve(` `int` `idx, ` `int` `itemWt, ` `int` `wts[],` ` ` `int` `N)` `{` ` ` `if` `(found)` ` ` `return` `;` ` ` ` ` `// Item has been measured` ` ` `if` `(itemWt == 0) {` ` ` `found = 1;` ` ` `return` `;` ` ` `}` ` ` ` ` `// If the index of current weight` ` ` `// is greater than totalWts` ` ` `if` `(idx > N)` ` ` `return` `;` ` ` ` ` `// Current weight is not included` ` ` `// on either side` ` ` `solve(idx + 1, itemWt, wts, N);` ` ` ` ` `// Current weight is included on the` ` ` `// side containing item` ` ` `solve(idx + 1, itemWt + wts[idx], wts,` ` ` `N);` ` ` ` ` `// Current weight is included on the` ` ` `// side opposite to the side` ` ` `// containing item` ` ` `solve(idx + 1, itemWt - wts[idx], wts,` ` ` `N);` `}` ` ` `// This function computes the required array` `// of weights using a` `bool` `checkItem(` `int` `a, ` `int` `W)` `{` ` ` `// If the a is 2 or 3, answer always` ` ` `// exists` ` ` `if` `(a == 2 || a == 3)` ` ` `return` `1;` ` ` ` ` `int` `wts[100]; ` `// weights array` ` ` `int` `totalWts = 0; ` `// feasible weights` ` ` `wts[0] = 1;` ` ` `for` `(` `int` `i = 1;; i++) {` ` ` `wts[i] = wts[i - 1] * a;` ` ` `totalWts++;` ` ` ` ` `// if the current weight` ` ` `// becomes greater than 1e9` ` ` `// break from the loop` ` ` `if` `(wts[i] > 1e9)` ` ` `break` `;` ` ` `}` ` ` `solve(0, W, wts, totalWts);` ` ` `if` `(found)` ` ` `return` `1;` ` ` ` ` `// Item can't be measured` ` ` `return` `0;` `}` ` ` `// Driver Code to test above functions` `int` `main()` `{` ` ` `int` `a = 2, W = 5;` ` ` `if` `(checkItem(a, W))` ` ` `cout << ` `"YES"` `<< endl;` ` ` `else` ` ` `cout << ` `"NO"` `<< endl;` ` ` ` ` `a = 4, W = 11, found = 0;` ` ` `if` `(checkItem(a, W))` ` ` `cout << ` `"YES"` `<< endl;` ` ` `else` ` ` `cout << ` `"NO"` `<< endl;` ` ` ` ` `a = 4, W = 7, found = 0;` ` ` `if` `(checkItem(a, W))` ` ` `cout << ` `"YES"` `<< endl;` ` ` `else` ` ` `cout << ` `"NO"` `<< endl;` ` ` `return` `0;` `}` |

## Java

`// Java Program to check whether an item` `// can be measured using some weight and` `// a weighing scale.` ` ` `class` `GFG {` ` ` `// Variable to denote that answer has` `// been found` ` ` `static` `int` `found = ` `0` `;` ` ` ` ` `static` `void` `solve(` `int` `idx, ` `int` `itemWt, ` `int` `wts[], ` `int` `N) {` ` ` `if` `(found == ` `1` `) {` ` ` `return` `;` ` ` `}` ` ` ` ` `// Item has been measured` ` ` `if` `(itemWt == ` `0` `) {` ` ` `found = ` `1` `;` ` ` `return` `;` ` ` `}` ` ` ` ` `// If the index of current weight` ` ` `// is greater than totalWts` ` ` `if` `(idx > N) {` ` ` `return` `;` ` ` `}` ` ` ` ` `// Current weight is not included` ` ` `// on either side` ` ` `solve(idx + ` `1` `, itemWt, wts, N);` ` ` ` ` `// Current weight is included on the` ` ` `// side containing item` ` ` `solve(idx + ` `1` `, itemWt + wts[idx], wts,` ` ` `N);` ` ` ` ` `// Current weight is included on the` ` ` `// side opposite to the side` ` ` `// containing item` ` ` `solve(idx + ` `1` `, itemWt - wts[idx], wts,` ` ` `N);` ` ` `}` ` ` `// This function computes the required array` `// of weights using a` ` ` `static` `boolean` `checkItem(` `int` `a, ` `int` `W) {` ` ` `// If the a is 2 or 3, answer always` ` ` `// exists` ` ` `if` `(a == ` `2` `|| a == ` `3` `) {` ` ` `return` `true` `;` ` ` `}` ` ` ` ` `int` `wts[] = ` `new` `int` `[` `100` `]; ` `// weights array` ` ` `int` `totalWts = ` `0` `; ` `// feasible weights` ` ` `wts[` `0` `] = ` `1` `;` ` ` `for` `(` `int` `i = ` `1` `;; i++) {` ` ` `wts[i] = wts[i - ` `1` `] * a;` ` ` `totalWts++;` ` ` ` ` `// if the current weight` ` ` `// becomes greater than 1e9` ` ` `// break from the loop` ` ` `if` `(wts[i] > 1e9) {` ` ` `break` `;` ` ` `}` ` ` `}` ` ` `solve(` `0` `, W, wts, totalWts);` ` ` `if` `(found == ` `1` `) {` ` ` `return` `true` `;` ` ` `}` ` ` ` ` `// Item can't be measured` ` ` `return` `false` `;` ` ` `}` ` ` `// Driver Code to test above functions` ` ` `public` `static` `void` `main(String[] args) {` ` ` ` ` `int` `a = ` `2` `, W = ` `5` `;` ` ` `if` `(checkItem(a, W)) {` ` ` `System.out.println(` `"YES"` `);` ` ` `} ` `else` `{` ` ` `System.out.println(` `"NO"` `);` ` ` `}` ` ` ` ` `a = ` `4` `; W = ` `11` `;found = ` `0` `;` ` ` `if` `(checkItem(a, W)) {` ` ` `System.out.println(` `"YES"` `);` ` ` `} ` `else` `{` ` ` `System.out.println(` `"NO"` `);` ` ` `}` ` ` ` ` `a = ` `4` `; W = ` `7` `; found = ` `0` `;` ` ` `if` `(checkItem(a, W)) {` ` ` `System.out.println(` `"YES"` `);` ` ` `} ` `else` `{` ` ` `System.out.println(` `"NO"` `);` ` ` `}` ` ` `}` `}` ` ` `//this code contributed by Rajput-Ji` |

## C#

` ` `// C# Program to check whether an item` `// can be measured using some weight and` `// a weighing scale.` `using` `System; ` `public` `class` `GFG {` ` ` `// Variable to denote that answer has` `// been found` ` ` `static` `int` `found = 0;` ` ` ` ` `static` `void` `solve(` `int` `idx, ` `int` `itemWt, ` `int` `[]wts, ` `int` `N) {` ` ` `if` `(found == 1) {` ` ` `return` `;` ` ` `}` ` ` ` ` `// Item has been measured` ` ` `if` `(itemWt == 0) {` ` ` `found = 1;` ` ` `return` `;` ` ` `}` ` ` ` ` `// If the index of current weight` ` ` `// is greater than totalWts` ` ` `if` `(idx > N) {` ` ` `return` `;` ` ` `}` ` ` ` ` `// Current weight is not included` ` ` `// on either side` ` ` `solve(idx + 1, itemWt, wts, N);` ` ` ` ` `// Current weight is included on the` ` ` `// side containing item` ` ` `solve(idx + 1, itemWt + wts[idx], wts,` ` ` `N);` ` ` ` ` `// Current weight is included on the` ` ` `// side opposite to the side` ` ` `// containing item` ` ` `solve(idx + 1, itemWt - wts[idx], wts,` ` ` `N);` ` ` `}` ` ` `// This function computes the required array` `// of weights using a` ` ` `static` `bool` `checkItem(` `int` `a, ` `int` `W) {` ` ` `// If the a is 2 or 3, answer always` ` ` `// exists` ` ` `if` `(a == 2 || a == 3) {` ` ` `return` `true` `;` ` ` `}` ` ` ` ` `int` `[]wts = ` `new` `int` `[100]; ` `// weights array` ` ` `int` `totalWts = 0; ` `// feasible weights` ` ` `wts[0] = 1;` ` ` `for` `(` `int` `i = 1;; i++) {` ` ` `wts[i] = wts[i - 1] * a;` ` ` `totalWts++;` ` ` ` ` `// if the current weight` ` ` `// becomes greater than 1e9` ` ` `// break from the loop` ` ` `if` `(wts[i] > 1e9) {` ` ` `break` `;` ` ` `}` ` ` `}` ` ` `solve(0, W, wts, totalWts);` ` ` `if` `(found == 1) {` ` ` `return` `true` `;` ` ` `}` ` ` ` ` `// Item can't be measured` ` ` `return` `false` `;` ` ` `}` ` ` `// Driver Code to test above functions` ` ` `public` `static` `void` `Main() {` ` ` ` ` `int` `a = 2, W = 5;` ` ` `if` `(checkItem(a, W)) {` ` ` `Console.WriteLine(` `"YES"` `);` ` ` `} ` `else` `{` ` ` `Console.WriteLine(` `"NO"` `);` ` ` `}` ` ` ` ` `a = 4; W = 11;found = 0;` ` ` `if` `(checkItem(a, W)) {` ` ` `Console.WriteLine(` `"YES"` `);` ` ` `} ` `else` `{` ` ` `Console.WriteLine(` `"NO"` `);` ` ` `}` ` ` ` ` `a = 4; W = 7; found = 0;` ` ` `if` `(checkItem(a, W)) {` ` ` `Console.WriteLine(` `"YES"` `);` ` ` `} ` `else` `{` ` ` `Console.WriteLine(` `"NO"` `);` ` ` `}` ` ` `}` `}` ` ` `//this code contributed by Rajput-Ji` |

**Output:**

YES YES NO

**Time Complexity:** O(3^{N}), where N cannot be more than 20 since 4^{20} is greater than 10^{9}

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. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.