Given an integer N, the task is to count all possible N digit numbers such that A + reverse(A) = 10N – 1 where A is an N digit number and reverse(A) is reverse of A. A shouldn’t have any leading 0s.
Input: N = 2
All possible 2 digit numbers are 90, 81, 72, 63, 54, 45, 36, 27 and 18.
Input: N = 4
Approach: First we have to conclude that if N is odd then there is no number which will satisfy the given condition, let’s prove it for N = 3,
so and .
which is impossible as it is a floating point number.
Now Find answer for when N is even. For example, N=4,
and now if x + y = 9 then the number of pairs which satisfy this condition are 10.
(0, 9), (1, 8), (2, 7), (3, 6), (4, 5), (5, 4), (6, 3), (7, 2), (8, 1), (9, 0)
Now, 1st and Nth digit cannot have the pair (0, 9) as there shouldn’t be any leading 0s in Abut for all the remaining N/2-1 pairs there can be 10 pairs.
So the answer is , As N is large so we will print 9 followed by N/2-1 number of 0s.
Below is the implementation of the above approach:
Time Complexity: O(N)
- Count of N digit numbers possible which satisfy the given conditions
- Count of sub-sequences which satisfy the given condition
- Count of indices in an array that satisfy the given condition
- Count index pairs which satisfy the given condition
- Count triplet pairs (A, B, C) of points in 2-D space that satisfy the given condition
- Count of numbers in the range [L, R] which satisfy the given conditions
- Count of Numbers in Range where first digit is equal to last digit of the number
- Number of strings that satisfy the given condition
- Pairs from an array that satisfy the given condition
- Number of cells in a matrix that satisfy the given condition
- Append two elements to make the array satisfy the given condition
- Find the Number of Permutations that satisfy the given condition in an array
- Check if a cycle of length 3 exists or not in a graph that satisfy a given condition
- Count n digit numbers not having a particular digit
- Count numbers having 0 as a digit
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.