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 = reverse(N).
Therefore, the required output is 2
Input: D = 75
Output: 0
Approach: The problem can be solved based on the following observations:
N + D = reverse(N) => N – reverse(N) = D
=> D = ?(i=0 to [L/2]-1)(10L-1-i -10i )*( ni – nL-1-i), L = Count of digits in N.
If di = ni ? nL?1?i (0 ? i ? ?L/2? ? 1)
reverse(N) ? N = ?(i=0 to [L/2]-1) (10L-1-i -10i )*di
Follow the steps below to solve the problem:
- Let f(L, D) = reverse(N) ? N. Finding the count of N that satisfy the given formula is almost equivalent to enumerating the pairs of L and a sequence of integers in the range [?9, 9], { d0, d1, . . ., d?L/2??1 } (take into account that there is more than one N that corresponds to a sequence of di, though). Here, for any i such that 0 ? i < ?L/2? ? 1, the following holds:
10L?1?i ? 10i > ?(j=i+1 to [L/2 – 1]) ((10L?1?j ? 10j) · 9) + 10L??L/2?
- Let LD be the number of digits of D in decimal notation. Then, when L > 2LD and f(L, d) > 0, it can be seen that f(L, d) > D.
- Therefore, it is enough to consider the values between LD and 2LD as the value of L.
- For a fixed value of L, consider enumerating the sequences { d0, d1, …, d?L/2??1 } such that f(L, d) = D (and finding the count of N that satisfy the given formula), by performing Depth First Search starting from d0.
- When the values up to di?1 are already decided, it can be seen that there are at most two candidates of the value of di that have to be considered: the maximum value such that (10i ? 10L?1?i )di ? dif , and the minimum value such that (10i ? 10L?1?i )di > dif . (Intuitively, if the “halfway” value of f(L, d) during the search gets too far from D, it is not possible to “get back”, and thus it should “stay close” to D.)
- Therefore, for a fixed value of L, find the count of N that satisfy the given formula in O(2?L/2?) time.
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
int MAXL = 17;
int N;
vector<int> v;
int count(int D, int l, int t, int x[])
{
if (t == N) {
if (D != 0)
return 0;
long ans = 1;
for (int i = 0; i < N; i++) {
ans *= (i == 0 ? 9 : 10) - abs(x[i]);
}
if (l % 2 == 0) {
ans *= 10;
}
return ans;
}
long ans = 0;
for (int m = -9; m <= 9; m++) {
if (-v[t] < D + v[t] * m && D +
v[t] * m < v[t]) {
x[t] = m;
ans += count(D + v[t] * m, l,
t + 1, x);
}
}
return ans;
}
int findN(int D)
{
if (D % 9 != 0)
return 0;
D /= 9;
vector<int> B(MAXL);
B[0] = 1;
for (int i = 1; i < MAXL; i++) {
B[i] = B[i - 1] * 10;
}
int ans = 0;
for (int i = 1; i <= MAXL; i++) {
N = (i + 1) / 2;
v.clear();
v.resize(N);
for (int j = 0; j < N; j++)
for (int k = j; k < i - j; k++)
v[j] += B[k];
int arr[N];
ans += count(D, i, 0, arr);
}
return ans;
}
int main()
{
int D = 63;
cout << findN(D);
}
|
Java
import java.util.*;
public class Main {
static final int MAXL = 17;
static int N;
static long[] v;
static long findN(int D)
{
if (D % 9 != 0)
return 0;
D /= 9;
long[] B = new long[MAXL];
B[0] = 1;
for (int i = 1; i < MAXL; i++) {
B[i] = B[i - 1] * 10;
}
long ans = 0;
for (int i = 1; i <= MAXL; i++) {
N = (i + 1) / 2;
v = new long[N];
for (int j = 0; j < N; j++)
for (int k = j; k < i - j; k++)
v[j] += B[k];
ans += count(D, i, 0, new int[N]);
}
return ans;
}
static long count(long D, int l,
int t, int[] x)
{
if (t == N) {
if (D != 0)
return 0;
long ans = 1;
for (int i = 0; i < N; i++) {
ans *= (i == 0 ? 9 : 10)
- Math.abs(x[i]);
}
if (l % 2 == 0) {
ans *= 10;
}
return ans;
}
long ans = 0;
for (int m = -9; m <= 9; m++) {
if (-v[t] < D + v[t] * m
&& D + v[t] * m < v[t]) {
x[t] = m;
ans += count(D + v[t] * m,
l, t + 1, x);
}
}
return ans;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int D = 63;
System.out.println(findN(D));
sc.close();
}
}
|
Python3
MAXL = 17;
N = 0;
v = 0;
def findN(D):
global N;
global v;
if (D % 9 != 0):
return 0;
D //= 9;
B = [0]*MAXL;
B[0] = 1;
for i in range(1, MAXL):
B[i] = B[i - 1] * 10;
ans = 0;
for i in range(1, MAXL + 1):
N = (i + 1) // 2;
v = [0]*N;
for j in range(N):
for k in range(j, i - j):
v[j] += B[k];
temp = [0]*N;
ans += count(D, i, 0, temp);
return ans;
def count(D, l, t, x):
global N;
global v;
if (t == N):
if (D != 0):
return 0;
ans = 1;
for i in range(N):
ans *= (9 if i == 0 else 10) - abs(x[i]);
if (l % 2 == 0):
ans *= 10;
return ans;
ans = 0;
for m in range(-9, 10):
if (-v[t] < D + v[t] * m and D + v[t] * m < v[t]):
x[t] = m;
ans += count(D + v[t] * m, l, t + 1, x);
return ans;
if __name__ == '__main__':
D = 63;
print(findN(D));
|
C#
using System;
class GFG
{
static int MAXL = 17;
static int N;
static long[] v;
static long findN(int D)
{
if (D % 9 != 0)
return 0;
D /= 9;
long[] B = new long[MAXL];
B[0] = 1;
for (int i = 1; i < MAXL; i++)
{
B[i] = B[i - 1] * 10;
}
long ans = 0;
for (int i = 1; i <= MAXL; i++)
{
N = (i + 1) / 2;
v = new long[N];
for (int j = 0; j < N; j++)
for (int k = j; k < i - j; k++)
v[j] += B[k];
ans += count(D, i, 0, new int[N]);
}
return ans;
}
static long count(long D, int l,
int t, int[] x)
{
if (t == N)
{
if (D != 0)
return 0;
long ans = 1;
for (int i = 0; i < N; i++)
{
ans *= (i == 0 ? 9 : 10)
- Math.Abs(x[i]);
}
if (l % 2 == 0)
{
ans *= 10;
}
return ans;
}
long anss = 0;
for (int m = -9; m <= 9; m++)
{
if (-v[t] < D + v[t] * m
&& D + v[t] * m < v[t])
{
x[t] = m;
anss += count(D + v[t] * m,
l, t + 1, x);
}
}
return anss;
}
public static void Main(String[] args)
{
int D = 63;
Console.WriteLine(findN(D));
}
}
|
Javascript
<script>
let MAXL = 17;
let N;
let v = []
function findN(D)
{
if (D % 9 != 0)
return 0;
D /= 9;
let B = new Array(MAXL).fill(0);
B[0] = 1;
for (let i = 1; i < MAXL; i++) {
B[i] = B[i - 1] * 10;
}
let ans = 0;
for (let i = 1; i <= MAXL; i++) {
N = Math.floor((i + 1) / 2);
v = new Array(N).fill(0);
for (let j = 0; j < N; j++)
for (let k = j; k < i - j; k++)
v[j] += B[k];
ans += count(D, i, 0, new Array(N));
}
return ans;
}
function count(D, l, t, x)
{
if (t == N) {
if (D != 0)
return 0;
let ans = 1;
for (let i = 0; i < N; i++) {
ans *= (i == 0 ? 9 : 10)
- Math.abs(x[i]);
}
if (l % 2 == 0) {
ans *= 10;
}
return ans;
}
let ans = 0;
for (let m = -9; m <= 9; m++)
{
if (-v[t] < D + v[t] * m
&& D + v[t] * m < v[t])
{
x[t] = m;
ans += count(D + v[t] * m,
l, t + 1, x);
}
}
return ans;
}
let D = 63;
document.write(findN(D));
</script>
|
Time Complexity: O(2LD), where LD denotes the number of digits of D in decimal notation.
Auxiliary Space: O( LD)