Given an integer N, the task is to determine whether it is possible to obtain the value N from 1 by repetitively multiplying by 10 or 20. Print Yes if possible or No otherwise.
Examples:
Input: N = 200
Output: YES
Explanation:
1 * 10 -> 10 * 20 -> 200
Input: N = 90
Output: NO
Approach:
Follow the steps below to solve the problem:
- Count the number of trailing zeroes.
- After removing the trailing zeroes, if the remaining N cannot be expressed as a power of 2, print NO.
- Otherwise, if log2N <= Count of trailing zeroes, print Yes.
Below is the implementation of the above approach:
C++
// C++ program to check if N // can be obtained from 1 by // repetitive multiplication // by 10 or 20 #include<bits/stdc++.h> using namespace std; // Function to check if N can // be obtained or not void Is_possible(long long int N) { int C = 0; int D = 0; // Count and remove trailing // zeroes while (N % 10 == 0) { N = N / 10; C += 1; } // Check if remaining // N is a power of 2 if(pow(2, (int)log2(N)) == N) { D = (int)log2(N); // To check the condition // to print YES or NO if (C >= D) cout << "YES"; else cout << "NO"; } else cout << "NO"; } // Driver code int main() { long long int N = 2000000000000; Is_possible(N); } // This code is contributed by Stream_Cipher |
Java
// Java program to check if N // can be obtained from 1 by // repetitive multiplication // by 10 or 20 import java.util.*; class GFG{ static void Is_possible(long N) { long C = 0; long D = 0; // Count and remove trailing // zeroes while (N % 10 == 0) { N = N / 10; C += 1; } // Check if remaining // N is a power of 2 if(Math.pow(2, (long)(Math.log(N) / (Math.log(2)))) == N) { D = (long)(Math.log(N) / (Math.log(2))); // To check the condition // to prlong YES or NO if (C >= D) System.out.print("YES"); else System.out.print("NO"); } else System.out.print("NO"); } // Driver code public static void main(String args[]) { long N = 2000000000000L; Is_possible(N); } } // This code is contributed by Stream_Cipher |
Python
# Python Program to check if N # can be obtained from 1 by # repetitive multiplication # by 10 or 20 import math # Function to check if N can # be obtained or not def Is_possible(N): C = 0 D = 0 # Count and remove trailing # zeroes while ( N % 10 == 0): N = N / 10 C += 1 # Check if remaining # N is a power of 2 if ( math.log(N, 2) - int(math.log(N, 2)) == 0): D = int(math.log(N, 2)) # To check the condition # to print YES or NO if (C >= D): print("YES") else: print("NO") else: print("NO") # Driver Program N = 2000000000000Is_possible(N) |
C#
// C# program to check if N // can be obtained from 1 by // repetitive multiplication // by 10 or 20 using System; class GFG{ static void Is_possible(long N) { long C = 0; long D = 0; // Count and remove trailing // zeroes while (N % 10 == 0) { N = N / 10; C += 1; } // Check if remaining // N is a power of 2 if(Math.Pow(2, (long)(Math.Log(N) / (Math.Log(2)))) == N) { D = (long)(Math.Log(N) / (Math.Log(2))); // To check the condition // to prlong YES or NO if (C >= D) Console.WriteLine("YES"); else Console.WriteLine("NO"); } else Console.WriteLine("NO"); } // Driver Code public static void Main() { long N = 2000000000000L; Is_possible(N); } } // This code is contributed by Stream_Cipher |
YES
Time Complexity: O(log10(N))
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Check if sum of array can be reduced to zero by repetitively reducing array element by their index value
- Check if elements of array can be made equal by multiplying given prime numbers
- Check if N can be obtained by repetitive addition or subtraction of two given numbers
- Check if sum Y can be obtained from the Array by the given operations
- Check if K can be obtained by performing arithmetic operations on any permutation of an Array
- Maximum array sum that can be obtained after exactly k changes
- Total distinct pairs from two arrays such that second number can be obtained by inverting bits of first
- Minimum integer that can be obtained by swapping adjacent digits of different parity
- Smallest array that can be obtained by replacing adjacent pairs with their products
- Find the number of consecutive zero at the end after multiplying n numbers
- Reach A and B by multiplying them with K and K^2 at every step
- Maximize the sum of array by multiplying prefix of array with -1
- Minimum operations required to convert X to Y by multiplying X with the given co-primes
- Find Kth number from sorted array formed by multiplying any two numbers in the array
- Minimum steps to reach N from 1 by multiplying each step by 2, 3, 4 or 5
- Min operations to reduce N to 1 by multiplying by A or dividing by B
- Min operations to reduce N by multiplying by any number or taking square root
- Convert 1 into X in min steps by multiplying with 2 or 3 or by adding 1
- Maximum subsequence sum possible by multiplying each element by its index
- Construct a frequency array of digits of the values obtained from x^1, x^2, ........, x^n
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

