Check if a given number is one less than twice its reverse
Last Updated :
20 Oct, 2022
Given an integer N, the task is to check if it is a solution to the equation 2 * reverse(N) – 1 = N
Examples:
Input: N = 73
Output: Yes
Explanation:
2 * reverse(N) = 2 * 37 = 74
N + 1 = 73 + 1 = 74
Input: N = 83
Output: No
Naive Approach: The simplest approach is to find the reverse of the given number and check if it satisfies the equation 2 * reverse(N) = N + 1 or not and print “Yes” or “No” accordingly.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int rev( int num)
{
int rev_num = 0;
while (num > 0) {
rev_num
= rev_num * 10 + num % 10;
num = num / 10;
}
return rev_num;
}
bool check( int n)
{
return 2 * rev(n) == n + 1;
}
int main()
{
int n = 73;
if (check(n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int rev( int num)
{
int rev_num = 0 ;
while (num > 0 )
{
rev_num = rev_num * 10 +
num % 10 ;
num = num / 10 ;
}
return rev_num;
}
static boolean check( int n)
{
return 2 * rev(n) == n + 1 ;
}
public static void main(String[] args)
{
int n = 73 ;
if (check(n))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
def rev(num):
rev_num = 0
while (num > 0 ):
rev_num = (rev_num * 10 +
num % 10 )
num = num / / 10
return rev_num
def check(n):
return ( 2 * rev(n) = = n + 1 )
n = 73
if (check(n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static int rev( int num)
{
int rev_num = 0;
while (num > 0)
{
rev_num = rev_num * 10 +
num % 10;
num = num / 10;
}
return rev_num;
}
static bool check( int n)
{
return 2 * rev(n) == n + 1;
}
public static void Main(String[] args)
{
int n = 73;
if (check(n))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function rev(num)
{
var rev_num = 0;
while (num > 0) {
rev_num
= rev_num * 10 + num % 10;
num = parseInt(num / 10);
}
return rev_num;
}
function check(n)
{
return 2 * rev(n) == n + 1;
}
var n = 73;
if (check(n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(log10N)
Auxiliary Space: O(1)
Efficient Approach: The key observation to optimize the above approach is that the numbers satisfying the given equation can be represented by:
X = 8 * 10(n-1) – 7
To check if a number X satisfies the above equation, it needs to be checked if the number (X + 7) / 8 is a power of 10 or not.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPower( int x, int y)
{
int res1 = log (y) / log (x);
double res2 = log (y) / log (x);
return (res1 == res2);
}
bool check( int n)
{
int x = (n + 7) / 8;
if ((n + 7) % 8 == 0
&& isPower(10, x))
return true ;
else
return false ;
}
int main()
{
int n = 73;
if (check(n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static boolean isPower( int x, int y)
{
double res1 = Math.log(y) / Math.log(x);
double res2 = Math.log(y) / Math.log(x);
return (res1 == res2);
}
static boolean check( int n)
{
int x = (n + 7 ) / 8 ;
if ((n + 7 ) % 8 == 0 &&
isPower( 10 , x))
return true ;
else
return false ;
}
public static void main (String[] args)
{
int n = 73 ;
if (check(n))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
import math
def isPower(x, y):
res1 = math.log(y) / / math.log(x)
res2 = math.log(y) / / math.log(x)
return (res1 = = res2)
def check(n):
x = (n + 7 ) / / 8
if ((n + 7 ) % 8 = = 0 and
isPower( 10 , x)):
return True
else :
return False
n = 73
if (check(n) ! = 0 ):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static bool isPower( int x, int y)
{
double res1 = Math.Log(y) / Math.Log(x);
double res2 = Math.Log(y) / Math.Log(x);
return (res1 == res2);
}
static bool check( int n)
{
int x = (n + 7) / 8;
if ((n + 7) % 8 == 0 &&
isPower(10, x))
return true ;
else
return false ;
}
static public void Main ()
{
int n = 73;
if (check(n))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function isPower(x, y)
{
let res1 = Math.log(y) / Math.log(x);
let res2 = Math.log(y) / Math.log(x);
return (res1 == res2);
}
function check(n)
{
let x = (n + 7) / 8;
if ((n + 7) % 8 == 0 &&
isPower(10, x))
return true ;
else
return false ;
}
let n = 73;
if (check(n) != 0)
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...