# Check if N can be represented as sum of distinct powers of 3

Given a positive integer **N**, the task is to check whether the given number **N **can be represented as the sum of the **distinct** **powers** of **3**. If found to be true, then print **“Yes”**. Otherwise, **“No”**.

**Examples:**

Input:N =28Output:YesExplanation:

The number N(= 28) can be represented (1 + 7) = (3^{0}+ 3^{3}), which is a perfect power 2.

Input:N = 6Output:No

**Approach: **The simplest approach to solve the given problem is to generate all possible permutations of all **distinct powers **of **3** and if there exists any such combination whose sum is a perfect power of **3**. As **3 ^{15} > 10^{7}** so there are only

**16**distinct powers i.e.,

**[0, 15]**.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to try all permutations` `// of distinct powers` `bool` `PermuteAndFind(vector<` `long` `> power, ` `int` `idx,` ` ` `long` `SumSoFar, ` `int` `target)` `{` ` ` `// Base Case` ` ` `if` `(idx == power.size()) {` ` ` `// If the distinct powers` ` ` `// sum is obtained` ` ` `if` `(SumSoFar == target)` ` ` `return` `true` `;` ` ` `// Otherwise` ` ` `return` `false` `;` ` ` `}` ` ` `// If current element not selected` ` ` `// in power[]` ` ` `bool` `select` ` ` `= PermuteAndFind(power, idx + 1, SumSoFar, target);` ` ` `// If current element selected in` ` ` `// power[]` ` ` `bool` `notselect = PermuteAndFind(` ` ` `power, idx + 1, SumSoFar + power[idx], target);` ` ` `// Return 1 if any permutation` ` ` `// found` ` ` `return` `(select || notselect);` `}` `// Function to check the N can be` `// represented as the sum of the` `// distinct powers of 3` `void` `DistinctPowersOf3(` `int` `N)` `{` ` ` `// Stores the all distincts powers` ` ` `// of three to [0, 15]` ` ` `vector<` `long` `> power(16);` ` ` `power[0] = 1;` ` ` `for` `(` `int` `i = 1; i < 16; i++)` ` ` `power[i] = 3 * power[i - 1];` ` ` `// Function Call` ` ` `bool` `found = PermuteAndFind(power, 0, 0L, N);` ` ` `// print` ` ` `if` `(found == ` `true` `) {` ` ` `cout << ` `"Yes"` `;` ` ` `}` ` ` `else` `{` ` ` `cout << ` `"No"` `;` ` ` `}` `}` `// Driven Code` `int` `main()` `{` ` ` `int` `N = 91;` ` ` `DistinctPowersOf3(N);` ` ` `return` `0;` `}` |

## Java

`// Java program for the above approach` `class` `GFG` `{` ` ` ` ` `// Function to try all permutations` ` ` `// of distinct powers` ` ` `public` `static` `boolean` `PermuteAndFind(` `int` `[] power, ` `int` `idx,` ` ` `int` `SumSoFar, ` `int` `target)` ` ` `{` ` ` `// Base Case` ` ` `if` `(idx == power.length)` ` ` `{` ` ` `// If the distinct powers` ` ` `// sum is obtained` ` ` `if` `(SumSoFar == target)` ` ` `return` `true` `;` ` ` `// Otherwise` ` ` `return` `false` `;` ` ` `}` ` ` `// If current element not selected` ` ` `// in power[]` ` ` `boolean` `select = PermuteAndFind(power, idx + ` `1` `, SumSoFar, target);` ` ` `// If current element selected in` ` ` `// power[]` ` ` `boolean` `notselect = PermuteAndFind(power, idx + ` `1` `, SumSoFar + power[idx], target);` ` ` `// Return 1 if any permutation` ` ` `// found` ` ` `return` `(select || notselect);` ` ` `}` ` ` `// Function to check the N can be` ` ` `// represented as the sum of the` ` ` `// distinct powers of 3` ` ` `public` `static` `void` `DistinctPowersOf3(` `int` `N)` ` ` `{` ` ` ` ` `// Stores the all distincts powers` ` ` `// of three to [0, 15]` ` ` `int` `[] power = ` `new` `int` `[` `16` `];` ` ` `power[` `0` `] = ` `1` `;` ` ` `for` `(` `int` `i = ` `1` `; i < ` `16` `; i++)` ` ` `power[i] = ` `3` `* power[i - ` `1` `];` ` ` `// Function Call` ` ` `boolean` `found = PermuteAndFind(power, ` `0` `, ` `0` `, N);` ` ` `// print` ` ` `if` `(found == ` `true` `) {` ` ` `System.out.println(` `"Yes"` `);` ` ` `} ` `else` `{` ` ` `System.out.println(` `"No"` `);` ` ` `}` ` ` `}` ` ` `// Driven Code` ` ` `public` `static` `void` `main(String args[]) {` ` ` `int` `N = ` `91` `;` ` ` `DistinctPowersOf3(N);` ` ` `}` `}` `// This code is contributed by _saurabh_jaiswal.` |

## Python3

`# Python3 program for the above approach` `# Function to try all permutations` `# of distinct powers` `def` `PermuteAndFind(power, idx, SumSoFar, target):` ` ` ` ` `# Base Case` ` ` `if` `(idx ` `=` `=` `len` `(power)):` ` ` ` ` `# If the distinct powers` ` ` `# sum is obtained` ` ` `if` `(SumSoFar ` `=` `=` `target):` ` ` `return` `True` ` ` ` ` `# Otherwise` ` ` `return` `False` ` ` `# If current element not selected` ` ` `# in power[]` ` ` `select ` `=` `PermuteAndFind(power, idx ` `+` `1` `,` ` ` `SumSoFar, target)` ` ` `# If current element selected in` ` ` `# power[]` ` ` `notselect ` `=` `PermuteAndFind(power, idx ` `+` `1` `,` ` ` `SumSoFar ` `+` `power[idx],` ` ` `target)` ` ` `# Return 1 if any permutation` ` ` `# found` ` ` `return` `(select ` `or` `notselect)` `# Function to check the N can be` `# represented as the sum of the` `# distinct powers of 3` `def` `DistinctPowersOf3(N):` ` ` ` ` `# Stores the all distincts powers` ` ` `# of three to[0, 15]` ` ` `power ` `=` `[` `0` `for` `x ` `in` `range` `(` `16` `)]` ` ` `power[` `0` `] ` `=` `1` ` ` ` ` `for` `i ` `in` `range` `(` `1` `, ` `16` `):` ` ` `power[i] ` `=` `3` `*` `power[i ` `-` `1` `]` ` ` `# Function Call` ` ` `found ` `=` `PermuteAndFind(power, ` `0` `, ` `0` `, N)` ` ` `# Print` ` ` `if` `(found ` `=` `=` `True` `):` ` ` `print` `(` `"Yes"` `)` ` ` `else` `:` ` ` `print` `(` `"No"` `)` `# Driver Code` `N ` `=` `91` `DistinctPowersOf3(N)` `# This code is contributed by amreshkumar3` |

## Javascript

`<script>` `// Javascript program for the above approach` `// Function to try all permutations` `// of distinct powers` `function` `PermuteAndFind(power, idx, SumSoFar, target) {` ` ` `// Base Case` ` ` `if` `(idx == power.length) {` ` ` `// If the distinct powers` ` ` `// sum is obtained` ` ` `if` `(SumSoFar == target) ` `return` `true` `;` ` ` `// Otherwise` ` ` `return` `false` `;` ` ` `}` ` ` `// If current element not selected` ` ` `// in power[]` ` ` `let select = PermuteAndFind(power, idx + 1, SumSoFar, target);` ` ` `// If current element selected in` ` ` `// power[]` ` ` `let notselect = PermuteAndFind(power, idx + 1, SumSoFar + power[idx], target);` ` ` `// Return 1 if any permutation` ` ` `// found` ` ` `return` `select || notselect;` `}` `// Function to check the N can be` `// represented as the sum of the` `// distinct powers of 3` `function` `DistinctPowersOf3(N) {` ` ` `// Stores the all distincts powers` ` ` `// of three to [0, 15]` ` ` `let power = ` `new` `Array(16);` ` ` `power[0] = 1;` ` ` `for` `(let i = 1; i < 16; i++) power[i] = 3 * power[i - 1];` ` ` `// Function Call` ` ` `let found = PermuteAndFind(power, 0, 0, N);` ` ` `// print` ` ` `if` `(found == ` `true` `) {` ` ` `document.write(` `"Yes"` `);` ` ` `} ` `else` `{` ` ` `document.write(` `"No"` `);` ` ` `}` `}` `// Driven Code` `let N = 91;` `DistinctPowersOf3(N);` `</script>` |

**Output**

Yes

* Time Complexity: *O(2

^{N})

*O(2*

**Auxiliary Space:**^{N})

**Another Approach: **The above approach can also be optimized by observing the fact that **N** in **ternary** form** **(**Base 3**). Each digit is **3 ^{i}**, and the

**ternary number(N)**is the sum of them.

To have **distinct** powers of **3**, to sum up to **N, **in ternary form the **i ^{th} **digit can be either

**0,1 or 2**(In Binary, it is 0 and 1). Thus, there can be three cases for each digit at

**ith**position

**:**

- Digit
**can**be**0**i.e. there is**No****3**number in^{i}**N**. - Digit
**can**be**1**i.e, there is**One****3**number in^{i}**N**. - Digit
**cannot**be**2**because then there are**2 of 3**, therefore,^{i}**not distinct**.

Follow the below steps to solve the problem:

- Iterate until
**N**becomes**0**and perform the following steps:- If the value of
**N%3**is**2**, then print**“No”**. - Otherwise, divide
**N**by**3**.

- If the value of
- After completing the above steps, if the value of
**N**is**0**then print**“Yes”**. Otherwise,**“No”**.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to check whether the given` `// N can be represented as the sum of` `// the distinct powers of 3` `void` `DistinctPowersOf3(` `int` `N)` `{` ` ` `// Iterate until N is non-zero` ` ` `while` `(N > 0) {` ` ` `// Termination Condition` ` ` `if` `(N % 3 == 2) {` ` ` `cout << ` `"No"` `;` ` ` `return` `;` ` ` `}` ` ` `// Right shift ternary bits` ` ` `// by 1 for the next digit` ` ` `N /= 3;` ` ` `}` ` ` `// If N can be expressed as the` ` ` `// sum of perfect powers of 3` ` ` `cout << ` `"Yes"` `;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `N = 91;` ` ` `DistinctPowersOf3(N);` ` ` `return` `0;` `}` |

## Java

`// Java program for the above approach` `class` `GFG` `{` ` ` `// Function to check whether the given` `// N can be represented as the sum of` `// the distinct powers of 3` `public` `static` `void` `DistinctPowersOf3(` `int` `N)` `{` ` ` ` ` `// Iterate until N is non-zero` ` ` `while` `(N > ` `0` `) {` ` ` `// Termination Condition` ` ` `if` `(N % ` `3` `== ` `2` `) {` ` ` `System.out.println(` `"No"` `);` ` ` `return` `;` ` ` `}` ` ` `// Right shift ternary bits` ` ` `// by 1 for the next digit` ` ` `N /= ` `3` `;` ` ` `}` ` ` `// If N can be expressed as the` ` ` `// sum of perfect powers of 3` ` ` `System.out.println(` `"Yes"` `);` `}` `// Driver Code` `public` `static` `void` `main(String args[])` `{` ` ` `int` `N = ` `91` `;` ` ` `DistinctPowersOf3(N);` `}` `}` `// This code is contributed by _saurabh_jaiswal.` |

## Python3

`# Python3 program for the above approach` `# Function to check whether the given` `# N can be represented as the sum of` `# the distinct powers of 3` `def` `DistinctPowersOf3(N):` ` ` ` ` `# Iterate until N is non-zero` ` ` `while` `(N > ` `0` `):` ` ` `# Termination Condition` ` ` `if` `(N ` `%` `3` `=` `=` `2` `):` ` ` `cout << ` `"No"` ` ` `return` ` ` `# Right shift ternary bits` ` ` `# by 1 for the next digit` ` ` `N ` `/` `/` `=` `3` ` ` `# If N can be expressed as the` ` ` `# sum of perfect powers of 3` ` ` `print` `(` `"Yes"` `)` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` `N ` `=` `91` ` ` `DistinctPowersOf3(N)` `# This code is contributed by mohit kumar 29.` |

## Javascript

`<script>` `// Javascript program for the above approach` `// Function to check whether the given` `// N can be represented as the sum of` `// the distinct powers of 3` `function` `DistinctPowersOf3(N)` `{` ` ` `// Iterate until N is non-zero` ` ` `while` `(N > 0) {` ` ` ` ` `// Termination Condition` ` ` `if` `(N % 3 == 2)` ` ` `{` ` ` `document.write(` `"No"` `);` ` ` `return` `;` ` ` `}` ` ` ` ` `// Right shift ternary bits` ` ` `// by 1 for the next digit` ` ` `N = Math.floor(N/ 3);` ` ` `}` ` ` ` ` `// If N can be expressed as the` ` ` `// sum of perfect powers of 3` ` ` `document.write(` `"Yes"` `);` `}` `// Driver Code` `let N = 91;` `DistinctPowersOf3(N);` `// This code is contributed by patel2127` `</script>` |

**Output**

Yes

**Time Complexity:** O(log_{3}N)**Auxiliary Space:** O(1)

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****.**