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 = 28
Output: Yes
Explanation:
The number N(= 28) can be represented (1 + 7) = (30 + 33), which is a perfect power 2.
Input: N = 6
Output: 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 315 > 107 so there are only 16 distinct powers i.e., [0, 15].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool PermuteAndFind(vector< long > power, int idx,
long SumSoFar, int target)
{
if (idx == power.size()) {
if (SumSoFar == target)
return true ;
return false ;
}
bool notselect
= PermuteAndFind(power, idx + 1, SumSoFar, target);
bool select = PermuteAndFind(
power, idx + 1, SumSoFar + power[idx], target);
return (select || notselect);
}
void DistinctPowersOf3( int N)
{
vector< long > power(16);
power[0] = 1;
for ( int i = 1; i < 16; i++)
power[i] = 3 * power[i - 1];
bool found = PermuteAndFind(power, 0, 0L, N);
if (found == true ) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
}
int main()
{
int N = 91;
DistinctPowersOf3(N);
return 0;
}
|
Java
class GFG
{
public static boolean PermuteAndFind( int [] power, int idx,
int SumSoFar, int target)
{
if (idx == power.length)
{
if (SumSoFar == target)
return true ;
return false ;
}
boolean notselect = PermuteAndFind(power, idx + 1 , SumSoFar, target);
boolean select = PermuteAndFind(power, idx + 1 , SumSoFar + power[idx], target);
return (select || notselect);
}
public static void DistinctPowersOf3( int N)
{
int [] power = new int [ 16 ];
power[ 0 ] = 1 ;
for ( int i = 1 ; i < 16 ; i++)
power[i] = 3 * power[i - 1 ];
boolean found = PermuteAndFind(power, 0 , 0 , N);
if (found == true ) {
System.out.println( "Yes" );
} else {
System.out.println( "No" );
}
}
public static void main(String args[]) {
int N = 91 ;
DistinctPowersOf3(N);
}
}
|
Python3
def PermuteAndFind(power, idx, SumSoFar, target):
if (idx = = len (power)):
if (SumSoFar = = target):
return True
return False
select = PermuteAndFind(power, idx + 1 ,
SumSoFar, target)
notselect = PermuteAndFind(power, idx + 1 ,
SumSoFar + power[idx],
target)
return (select or notselect)
def DistinctPowersOf3(N):
power = [ 0 for x in range ( 16 )]
power[ 0 ] = 1
for i in range ( 1 , 16 ):
power[i] = 3 * power[i - 1 ]
found = PermuteAndFind(power, 0 , 0 , N)
if (found = = True ):
print ( "Yes" )
else :
print ( "No" )
N = 91
DistinctPowersOf3(N)
|
C#
using System;
class GFG{
public static bool PermuteAndFind( int [] power, int idx,
int SumSoFar, int target)
{
if (idx == power.Length)
{
if (SumSoFar == target)
return true ;
return false ;
}
bool select = PermuteAndFind(power, idx + 1, SumSoFar, target);
bool notselect = PermuteAndFind(power, idx + 1, SumSoFar + power[idx], target);
return ( select || notselect);
}
public static void DistinctPowersOf3( int N)
{
int [] power = new int [16];
power[0] = 1;
for ( int i = 1; i < 16; i++)
power[i] = 3 * power[i - 1];
bool found = PermuteAndFind(power, 0, 0, N);
if (found == true ) {
Console.Write( "Yes" );
} else {
Console.Write( "No" );
}
}
public static void Main()
{
int N = 91;
DistinctPowersOf3(N);
}
}
|
Javascript
<script>
function PermuteAndFind(power, idx, SumSoFar, target) {
if (idx == power.length) {
if (SumSoFar == target) return true ;
return false ;
}
let select = PermuteAndFind(power, idx + 1, SumSoFar, target);
let notselect = PermuteAndFind(power, idx + 1, SumSoFar + power[idx], target);
return select || notselect;
}
function DistinctPowersOf3(N) {
let power = new Array(16);
power[0] = 1;
for (let i = 1; i < 16; i++) power[i] = 3 * power[i - 1];
let found = PermuteAndFind(power, 0, 0, N);
if (found == true ) {
document.write( "Yes" );
} else {
document.write( "No" );
}
}
let N = 91;
DistinctPowersOf3(N);
</script>
|
Time Complexity: O(2N)
Auxiliary Space: O(2N)
Another Approach: The above approach can also be optimized by observing the fact that N in ternary form (Base 3). Each digit is 3i, 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 ith 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 3i number in N.
- Digit can be 1 i.e, there is One 3i number in N.
- Digit cannot be 2 because then there are 2 of 3i, therefore, 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.
- 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++
#include <bits/stdc++.h>
using namespace std;
void DistinctPowersOf3( int N)
{
while (N > 0) {
if (N % 3 == 2) {
cout << "No" ;
return ;
}
N /= 3;
}
cout << "Yes" ;
}
int main()
{
int N = 91;
DistinctPowersOf3(N);
return 0;
}
|
Java
class GFG
{
public static void DistinctPowersOf3( int N)
{
while (N > 0 ) {
if (N % 3 == 2 ) {
System.out.println( "No" );
return ;
}
N /= 3 ;
}
System.out.println( "Yes" );
}
public static void main(String args[])
{
int N = 91 ;
DistinctPowersOf3(N);
}
}
|
Python3
def DistinctPowersOf3(N):
while (N > 0 ):
if (N % 3 = = 2 ):
cout << "No"
return
N / / = 3
print ( "Yes" )
if __name__ = = '__main__' :
N = 91
DistinctPowersOf3(N)
|
C#
using System;
class GFG
{
static void DistinctPowersOf3( int N)
{
while (N > 0) {
if (N % 3 == 2) {
Console.Write( "No" );
return ;
}
N /= 3;
}
Console.Write( "Yes" );
}
public static void Main()
{
int N = 91;
DistinctPowersOf3(N);
}
}
|
Javascript
<script>
function DistinctPowersOf3(N)
{
while (N > 0) {
if (N % 3 == 2)
{
document.write( "No" );
return ;
}
N = Math.floor(N/ 3);
}
document.write( "Yes" );
}
let N = 91;
DistinctPowersOf3(N);
</script>
|
Time Complexity: O(log3N)
Auxiliary Space: O(1)
Last Updated :
26 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...