Given two positive numbers N and X, the task is to check if the given number N can be expressed as the sum of distinct powers of X. If found to be true, then print “Yes”, Otherwise, print “No”.
Examples:
Input: N = 10, X = 3
Output: Yes
Explanation:
The given value of N(= 10) can be written as (1 + 9) = 30 + 32. Since all the power of X(= 3) are distinct. Therefore, print Yes.Input: N= 12, X = 4
Output: No
Approach: The given problem can be solved by checking if the number N can be written in base X or not. Follow the steps below to solve the problem:
- Iterate a loop until the value of N is at least 0 and perform the following steps:
- Calculate the value of remainder rem when N is divided by X.
- If the value of rem is at least 2, then print “No” and return.
- Otherwise, update the value of N as N / X.
- After completing the above steps, if there doesn’t exist any termination, then print “Yes” as the result at N can be expressed in the distinct power of X.
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 if the number N// can be expressed as the sum of// different powers of X or notbool ToCheckPowerofX(int n, int x){ // While n is a positive number while (n > 0) { // Find the remainder int rem = n % x; // If rem is at least 2, then // representation is impossible if (rem >= 2) { return false; } // Divide the value of N by x n = n / x; } return true;}// Driver Codeint main(){ int N = 10, X = 3; if (ToCheckPowerofX(N, X)) { cout << "Yes"; } else { cout << "No"; } return 0;} |
Java
// Java program for the above approachimport java.io.*;import java.util.*;class GFG{// Function to check if the number N// can be expressed as the sum of// different powers of X or notstatic boolean ToCheckPowerofX(int n, int x){ // While n is a positive number while (n > 0) { // Find the remainder int rem = n % x; // If rem is at least 2, then // representation is impossible if (rem >= 2) { return false; } // Divide the value of N by x n = n / x; } return true;}// Driver Codepublic static void main (String[] args){ int N = 10, X = 3; if (ToCheckPowerofX(N, X)) { System.out.print("Yes"); } else { System.out.print("No"); }}}// This code is contributed by sanjoy_62 |
Python3
# Python3 program for the above approach# Function to check if the number N# can be expressed as the sum of# different powers of X or notdef ToCheckPowerofX(n, x): # While n is a positive number while (n > 0): # Find the remainder rem = n % x # If rem is at least 2, then # representation is impossible if (rem >= 2): return False # Divide the value of N by x n = n // x return True# Driver Codeif __name__ == '__main__': N = 10 X = 3 if (ToCheckPowerofX(N, X)): print("Yes") else: print("No") # This code is contributed by bgangwar59 |
C#
// C# program for the above approachusing System;class GFG{ // Function to check if the number N// can be expressed as the sum of// different powers of X or notstatic bool ToCheckPowerofX(int n, int x){ // While n is a positive number while (n > 0) { // Find the remainder int rem = n % x; // If rem is at least 2, then // representation is impossible if (rem >= 2) { return false; } // Divide the value of N by x n = n / x; } return true;}// Driver codepublic static void Main(String []args){ int N = 10, X = 3; if (ToCheckPowerofX(N, X)) { Console.Write("Yes"); } else { Console.Write("No"); }}}// This code is contributed by code_hunt, |
Javascript
<script>// JavaScripts program for the above approach// Function to check if the number N// can be expressed as the sum of// different powers of X or notfunction ToCheckPowerofX(n, x){ // While n is a positive number while (n > 0) { // Find the remainder var rem = n % x; // If rem is at least 2, then // representation is impossible if (rem >= 2) { return false; } // Divide the value of N by x n = n / x; } return true;}// Driver Code var N = 10, X = 3; if (ToCheckPowerofX(N, X)) { document.write("Yes"); } else { document.write("No"); }</script> |
Yes
Time Complexity: O(log N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
