Check if a given number is one less than twice its reverse

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program of the
// above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Iterative function to
// reverse digits of num
int rev(int num)
{
    int rev_num = 0;
  
    // Loop to extract all
    // digits of the number
    while (num > 0) {
        rev_num
            = rev_num * 10 + num % 10;
        num = num / 10;
    }
    return rev_num;
}
  
// Function to check if N
// satisfies given equation
bool check(int n)
{
    return 2 * rev(n) == n + 1;
}
  
// Driver Code
int main()
{
    int n = 73;
    if (check(n))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program of the
// above approach
import java.util.*;
class GFG{
  
// Iterative function to
// reverse digits of num
static int rev(int num)
{
  int rev_num = 0;
  
  // Loop to extract all
  // digits of the number
  while (num > 0
  {
    rev_num = rev_num * 10
              num % 10;
    num = num / 10;
  }
  return rev_num;
}
  
// Function to check if N
// satisfies given equation
static boolean check(int n)
{
  return 2 * rev(n) == n + 1;
}
  
// Driver Code
public static void main(String[] args)
{
  int n = 73;
  if (check(n))
    System.out.print("Yes");
  else
    System.out.print("No");
}
}
  
// This code is contributed by Princi Singh

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program of the above approach 
    
# Iterative function to 
# reverse digits of num 
def rev(num): 
       
    rev_num = 0
     
    # Loop to extract all 
    # digits of the number 
    while (num > 0): 
        rev_num = (rev_num * 10 + 
                       num % 10)
        num = num // 10
        
    return rev_num 
    
# Function to check if N 
# satisfies given equation 
def check(n):
      
    return (2 * rev(n) == n + 1
    
# Driver Code 
n = 73
  
if (check(n)): 
    print("Yes")   
else:
    print("No"
  
# This code is contributed by code_hunt

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program of the above approach
using System;
  
class GFG{
  
// Iterative function to
// reverse digits of num
static int rev(int num)
{
    int rev_num = 0;
      
    // Loop to extract all
    // digits of the number
    while (num > 0) 
    {
        rev_num = rev_num * 10 + 
                      num % 10;
        num = num / 10;
    }
    return rev_num;
}
  
// Function to check if N
// satisfies given equation
static bool check(int n)
{
    return 2 * rev(n) == n + 1;
}
  
// Driver Code
public static void Main(String[] args)
{
    int n = 73;
      
    if (check(n))
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
  
// This code is contributed by Amit Katiyar

chevron_right


Output: 

Yes

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to implement
// the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to check y is a power of x
bool isPower(int x, int y)
{
    // logarithm function to
    // calculate value
    int res1 = log(y) / log(x);
    double res2 = log(y) / log(x);
  
    // Compare to the result1 or
    // result2 both are equal
    return (res1 == res2);
}
  
// Function to check if N
// satisfies the equation
// 2 * reverse(n) = n + 1
bool check(int n)
{
    int x = (n + 7) / 8;
    if ((n + 7) % 8 == 0
        && isPower(10, x))
        return true;
    else
        return false;
}
  
// Driver Code
int main()
{
    int n = 73;
    if (check(n))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement 
// the above approach 
import java.io.*;
import java.util.*;
  
class GFG{
    
// Function to check y is a power of x 
static boolean isPower(int x, int y) 
      
    // logarithm function to 
    // calculate value 
    double res1 = Math.log(y) / Math.log(x); 
    double res2 = Math.log(y) / Math.log(x); 
    
    // Compare to the result1 or 
    // result2 both are equal 
    return (res1 == res2); 
    
// Function to check if N 
// satisfies the equation 
// 2 * reverse(n) = n + 1 
static boolean check(int n) 
    int x = (n + 7) / 8
      
    if ((n + 7) % 8 == 0 && 
        isPower(10, x)) 
        return true
    else
        return false
    
// Driver Code 
public static void main (String[] args) 
    int n = 73
      
    if (check(n)) 
        System.out.print("Yes"); 
    else
        System.out.print("No"); 
}
}
  
// This code is contributed by code_hunt

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to implement
# the above approach
import math
  
# Function to check y is a power of x 
def isPower(x, y): 
      
    # logarithm function to 
    # calculate value 
    res1 = math.log(y) // math.log(x) 
    res2 = math.log(y) // math.log(x) 
    
    # Compare to the result1 or 
    # result2 both are equal 
    return (res1 == res2) 
   
# Function to check if N 
# satisfies the equation 
# 2 * reverse(n) = n + 1 
def check(n):
      
    x = (n + 7) // 8 
      
    if ((n + 7) % 8 == 0 and 
        isPower(10, x)): 
        return True 
    else:
        return False 
   
# Driver Code 
n = 73
  
if (check(n) != 0):
    print("Yes")
else:
    print("No"
  
# This code is contributed by code_hunt

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to implement 
// the above approach 
using System;
  
class GFG{
    
// Function to check y is a power of x 
static bool isPower(int x, int y) 
      
    // logarithm function to 
    // calculate value 
    double res1 = Math.Log(y) / Math.Log(x); 
    double res2 = Math.Log(y) / Math.Log(x); 
    
    // Compare to the result1 or 
    // result2 both are equal 
    return (res1 == res2); 
    
// Function to check if N 
// satisfies the equation 
// 2 * reverse(n) = n + 1 
static bool check(int n) 
    int x = (n + 7) / 8;
      
    if ((n + 7) % 8 == 0 && 
        isPower(10, x)) 
        return true
    else
        return false
    
// Driver Code 
static public void Main () 
    int n = 73; 
      
    if (check(n)) 
        Console.Write("Yes"); 
    else
        Console.Write("No"); 
}
}
  
// This code is contributed by code_hunt

chevron_right


Output: 

Yes

Time Complexity: O(log 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.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

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.