Check if N can be represented as sum of distinct powers of 3 | Java 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
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 ) ; } }