Check if N can be obtained from 1 by repetitively multiplying by 10 or 20
Last Updated :
22 Apr, 2021
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++
#include<bits/stdc++.h>
using namespace std;
void Is_possible( long long int N)
{
int C = 0;
int D = 0;
while (N % 10 == 0)
{
N = N / 10;
C += 1;
}
if ( pow (2, ( int )log2(N)) == N)
{
D = ( int )log2(N);
if (C >= D)
cout << "YES" ;
else
cout << "NO" ;
}
else
cout << "NO" ;
}
int main()
{
long long int N = 2000000000000;
Is_possible(N);
}
|
Java
import java.util.*;
class GFG{
static void Is_possible( long N)
{
long C = 0 ;
long D = 0 ;
while (N % 10 == 0 )
{
N = N / 10 ;
C += 1 ;
}
if (Math.pow( 2 , ( long )(Math.log(N) /
(Math.log( 2 )))) == N)
{
D = ( long )(Math.log(N) / (Math.log( 2 )));
if (C >= D)
System.out.print( "YES" );
else
System.out.print( "NO" );
}
else
System.out.print( "NO" );
}
public static void main(String args[])
{
long N = 2000000000000L;
Is_possible(N);
}
}
|
Python
import math
def Is_possible(N):
C = 0
D = 0
while ( N % 10 = = 0 ):
N = N / 10
C + = 1
if ( math.log(N, 2 )
- int (math.log(N, 2 )) = = 0 ):
D = int (math.log(N, 2 ))
if (C > = D):
print ( "YES" )
else :
print ( "NO" )
else :
print ( "NO" )
N = 2000000000000
Is_possible(N)
|
C#
using System;
class GFG{
static void Is_possible( long N)
{
long C = 0;
long D = 0;
while (N % 10 == 0)
{
N = N / 10;
C += 1;
}
if (Math.Pow(2, ( long )(Math.Log(N) /
(Math.Log(2)))) == N)
{
D = ( long )(Math.Log(N) / (Math.Log(2)));
if (C >= D)
Console.WriteLine( "YES" );
else
Console.WriteLine( "NO" );
}
else
Console.WriteLine( "NO" );
}
public static void Main()
{
long N = 2000000000000L;
Is_possible(N);
}
}
|
Javascript
<script>
function Is_possible( N)
{
let C = 0;
let D = 0;
while (N % 10 == 0)
{
N = N / 10;
C += 1;
}
if (Math.pow(2, (Math.log(N) /
(Math.log(2)))) == N)
{
D = (Math.log(N) / (Math.log(2)));
if (C >= D)
document.write( "YES" );
else
document.write( "NO" );
}
else
document.write( "NO" );
}
let N = 2000000000000;
Is_possible(N);
</script>
|
Time Complexity: O(log10(N))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...