Given two integers A and B, the task is to calculate the number of pairs (a, b) such that 1 ≤ a ≤ A, 1 ≤ b ≤ B and the equation (a * b) + a + b = concat(a, b) is true where conc(a, b) is the concatenation of a and b (for example, conc(12, 23) = 1223, conc(100, 11) = 10011). Note that a and b should not contain any leading zeroes.
Input: A = 1, B = 12 Output: 1 There exists only one pair (1, 9) satisfying the equation ((1 * 9) + 1 + 9 = 19)
Input: A = 2, B = 8 Output: 0 There doesn’t exist any pair satisfying the equation.
Approach: It can be observed that the above (a * b + a + b = conc(a, b)) will only be satisfied when the digits of an integer ≤ b contains only 9. Simply, calculate the number of digits (≤ b) containing only 9 and multiply with the integer a.
Below is the implementation of the above approach:
Please Login to comment...