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 = 792Output :10Explanation :

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 = 0Output: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 givenXis 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;` `}` |

*chevron_right*

*filter_none*

## 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` |

*chevron_right*

*filter_none*

## 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` |

*chevron_right*

*filter_none*

## 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` |

*chevron_right*

*filter_none*

**Output**

10

**Time Complexity:** O(1)**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:

- Find N numbers such that a number and its reverse are divisible by sum of its digits
- Node having maximum number of nodes less than its value in its subtree
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Count of Array elements greater than all elements on its left and next K elements on its right
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count the elements having frequency equals to its value
- Count sequences of length K having each term divisible by its preceding term
- Count of subarrays having sum equal to its length
- Count of permutations of an Array having each element as a multiple or a factor of its index
- Count 1s in binary matrix having remaining indices of its row and column filled with 0s
- Count right angled triangles in a matrix having two of its sides parallel to sides of the matrix
- Count of subarrays having sum equal to its length | Set 2
- Generate array having differences between count of occurrences of every array element on its left and right
- Count of groups having largest size while grouping according to sum of its digits
- Count numbers with difference between number and its digit sum greater than specific value
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Reverse a Linked List according to its Size
- Check if the product of digit sum and its reverse equals the number or not
- Check if a given number is one less than twice its reverse
- Count of numbers upto N having absolute difference of at most K between any two adjacent digits

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.