Count three-digit numbers having difference X with its reverse
Last Updated :
28 Apr, 2021
Given an integer X, the task is to count the total number of three-digit numbers having difference X with its reverse. If no such number exists, then print -1.
Examples:
Input: X = 792
Output : 10
Explanation :
901 - 109 = 792
911 - 119 = 792
921 - 129 = 792
931 - 139 = 792
941 - 149 = 792
951 - 159 = 792
961 - 169 = 792
971 - 179 = 792
981 - 189 = 792
991 - 199 = 792
Input: X = 0
Output: 90
Approach: The given problem can be solved based on the following observations:
Let N = rpq
Therefore, N = 100r + 10q + p
Therefore, reverse of N = 100p + 10q + r
Therefore, the problem reduces to solving (100r + 10q + p) – (r + 10q + 100p) = X
-> 99(r – p) = X
-> r - p = X / 99
Therefore, if given X is a multiple of 99, then solution exists.
Follow the steps below to solve the problem based on the above observations:
- Check if X is multiple of 99 or not. If not found to be true, print -1 as no solution exists.
- Otherwise, calculate X / 99. Generate all pairs using digits [1, 9] and for each pair, check if their difference is equal to X / 99 or not.
- If found to be true for any pair, increase count by 10, as the middle digit can be permuted to place any value from the range [0, 9] for the obtained pair.
- Finally, print the value of the count obtained.
Below is the implementation of the above approach:
C++
// C++ Program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to count three-digit
// numbers having difference x
// with its reverse
int Count_Number(int x)
{
int ans = 0;
// if x is not multiple of 99
if (x % 99 != 0) {
// No solution exists
ans = -1;
}
else {
int diff = x / 99;
// Generate all possible pairs
// of digits [1, 9]
for (int i = 1; i < 10; i++) {
for (int j = 1; j < 10; j++) {
// If any pair is obtained
// with difference x / 99
if ((i - j) == diff) {
// Increase count
ans += 10;
}
}
}
}
// Return the count
return ans;
}
// Driver Code
int main()
{
int x = 792;
cout << Count_Number(x) << endl;
return 0;
}
Java
// Java program to implement
// the above approach
import java.io.*;
import java.util.Arrays;
class GFG{
// Function to count three-digit
// numbers having difference x
// with its reverse
static int Count_Number(int x)
{
int ans = 0;
// If x is not multiple of 99
if (x % 99 != 0)
{
// No solution exists
ans = -1;
}
else
{
int diff = x / 99;
// Generate all possible pairs
// of digits [1, 9]
for(int i = 1; i < 10; i++)
{
for(int j = 1; j < 10; j++)
{
// If any pair is obtained
// with difference x / 99
if ((i - j) == diff)
{
// Increase count
ans += 10;
}
}
}
}
// Return the count
return ans;
}
// Driver Code
public static void main (String[] args)
{
int x = 792;
System.out.println(Count_Number(x));
}
}
// This code is contributed by sanjoy_62
Python3
# Python3 program to implement
# the above approach
# Function to count three-digit
# numbers having difference x
# with its reverse
def Count_Number(x):
ans = 0;
# If x is not multiple
# of 99
if (x % 99 != 0):
# No solution exists
ans = -1;
else:
diff = x / 99;
# Generate all possible pairs
# of digits [1, 9]
for i in range(1, 10):
for j in range(1, 10):
# If any pair is obtained
# with difference x / 99
if ((i - j) == diff):
# Increase count
ans += 10;
# Return the count
return ans;
# Driver Code
if __name__ == '__main__':
x = 792;
print(Count_Number(x));
# This code is contributed by shikhasingrajput
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to count three-digit
// numbers having difference x
// with its reverse
static int Count_Number(int x)
{
int ans = 0;
// If x is not multiple of 99
if (x % 99 != 0)
{
// No solution exists
ans = -1;
}
else
{
int diff = x / 99;
// Generate all possible pairs
// of digits [1, 9]
for(int i = 1; i < 10; i++)
{
for(int j = 1; j < 10; j++)
{
// If any pair is obtained
// with difference x / 99
if ((i - j) == diff)
{
// Increase count
ans += 10;
}
}
}
}
// Return the count
return ans;
}
// Driver Code
public static void Main()
{
int x = 792;
Console.WriteLine(Count_Number(x));
}
}
// This code is contributed by code_hunt
JavaScript
<script>
// Javascript program to implement
// the above approach
// Function to count three-digit
// numbers having difference x
// with its reverse
function Count_Number(x)
{
let ans = 0;
// If x is not multiple of 99
if (x % 99 != 0)
{
// No solution exists
ans = -1;
}
else
{
let diff = x / 99;
// Generate all possible pairs
// of digits [1, 9]
for(let i = 1; i < 10; i++)
{
for(let j = 1; j < 10; j++)
{
// If any pair is obtained
// with difference x / 99
if ((i - j) == diff)
{
// Increase count
ans += 10;
}
}
}
}
// Return the count
return ans;
}
// Driver code
let x = 792;
document.write(Count_Number(x));
// This code is contributed by splevel62
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Numbers having difference with digit sum more than s You are given two positive integer value n and s. You have to find the total number of such integer from 1 to n such that the difference of integer and its digit sum is greater than given s.Examples : Input : n = 20, s = 5 Output :11 Explanation : Integer from 1 to 9 have diff(integer - digitSum) =
7 min read
Count of integers having difference with its reverse equal to D Given an integer D, the task is to find the count of all possible positive integers N such that reverse(N) = N + D. Examples: Input: D = 63 Output: 2 Explanation: For N = 18, 18 + 63 = 81, which satisfies the condition N + D = reverse(N). For N = 29, 29 + 63 = 92, which satisfies the condition N + D
13 min read
Count numbers whose difference with N is equal to XOR with N Given a number N. The task is to count all possible values of x such that n\oplus x is equal to (N-x), where \oplus denotes bitwise XOR operation.Examples: Input: N = 3 Output: 4 The all possible values of x are respectively 0, 1, 2, 3. Input: N = 6 Output: 4 The all possible values of x are respect
4 min read
Count numbers up to N having digit D in its octal representation Given a positive integer N and an integer D representing a digit, the task is to count the numbers in the range[1, N] such that at least one digit in octal representation of the number is d. Examples: Input: N = 20, D = 7Output: 2Explanation: The numbers in the range [1, 20] having digit 7 in their
6 min read
Count of pairs in Array with difference equal to the difference with digits reversed Given an array arr[] of N integers, the task is to find the number of pairs of array elements (arr[i], arr[j]) such that the difference between the pairs is equal to the difference when the digits of both the numbers are reversed. Examples: Input: arr[] = {42, 11, 1, 97}Output: 2Explanation:The val
6 min read