Check if N can be represented as sum of distinct powers of 3 | C ++ program for the above approach ; Function to check whether the given N can be represented as the sum of the distinct powers of 3 ; Iterate until N is non - zero ; Termination Condition ; Right shift ternary bits by 1 for the next digit ; If N can be expressed as the sum of perfect powers of 3 ; Driver Code
#include <bits/stdc++.h> NEW_LINE 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 ; }