Given two numbers **A** and **B**, the task is to find the count of pairs **(X, Y)** in range **[A, B]**, such that **(X * Y) + (X + Y)** is equal to the number formed by concatenation of **X** and **Y**

**Examples:**

Input:A = 1, B = 9

Output:9

Explanation:

The pairs (1, 9), (2, 9), (3, 9), (4, 9), (5, 9), (6, 9), (7, 9), (8, 9) and (9, 9) are the required pairs.

Input:A = 4, B = 10

Output:7

Explanation:The pairs (4, 9), (5, 9), (6, 9), (7, 9), (8, 9), (9, 9) and (10, 9) satisfy the required condition.

**Approach :**

We can observe that any number of the form [9, 99, 999, 9999, ….] satisfies the condition with all other values.

Illustration:

If Y = 9, the required condition is satisfied for all values of X.

{1*9 + (1 + 9) = 19, 2*9 + (2 + 9) = 29, ……….. 11*9 + (11 + 9) = 119 …..

Similarly, for Y = 99, 1*99 + 1 + 99 = 199, 2*99 + 2 + 99 = 299, ………

Hence, follow the steps below to solve the problems:

- Count the number of possible values of Y of the form {9, 99, 999, 9999, ….} in range [A, B] and store in
**countY** - Count the number of possible values of X in the range [A, B] as
**countX**countX = (B - A + 1)

- The required count will be the product of possible count of X and Y, i.e.
answer = countX * countY

Below is the implementation of the above approach:

## C++

`// C++ program to count ` `// all the possible pairs ` `// with X*Y + (X + Y) equal to ` `// number formed by ` `// concatenating X and Y ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function for counting pairs ` `int` `countPairs(` `int` `A, ` `int` `B) ` `{ ` ` ` ` ` `int` `countY = 0, ` ` ` `countX = (B - A) + 1, ` ` ` `next_val = 9; ` ` ` ` ` `// Count possible vlues ` ` ` `// of Y ` ` ` `while` `(next_val <= B) { ` ` ` `if` `(next_val >= A) { ` ` ` `countY += 1; ` ` ` `} ` ` ` `next_val = next_val * 10 + 9; ` ` ` `} ` ` ` ` ` `return` `(countX * countY); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `A = 1; ` ` ` `int` `B = 16; ` ` ` `cout << countPairs(A, B); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count ` `// all the possible pairs ` `// with X*Y + (X + Y) equal to ` `// number formed by ` `// concatenating X and Y ` `import` `java.util.*; ` `class` `GFG{ ` ` ` `// Function for counting pairs ` `static` `int` `countPairs(` `int` `A, ` `int` `B) ` `{ ` ` ` `int` `countY = ` `0` `, ` ` ` `countX = (B - A) + ` `1` `, ` ` ` `next_val = ` `9` `; ` ` ` ` ` `// Count possible vlues ` ` ` `// of Y ` ` ` `while` `(next_val <= B) ` ` ` `{ ` ` ` `if` `(next_val >= A) ` ` ` `{ ` ` ` `countY += ` `1` `; ` ` ` `} ` ` ` `next_val = next_val * ` `10` `+ ` `9` `; ` ` ` `} ` ` ` `return` `(countX * countY); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `A = ` `1` `; ` ` ` `int` `B = ` `16` `; ` ` ` `System.out.print(countPairs(A, B)); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to count ` `# all the possible pairs ` `# with X*Y + (X + Y) equal to ` `# number formed by ` `# concatenating X and Y ` ` ` `# Function for counting pairs ` `def` `countPairs(A, B): ` ` ` ` ` `countY ` `=` `0` ` ` `countX ` `=` `(B ` `-` `A) ` `+` `1` ` ` `next_val ` `=` `9` ` ` ` ` `# Count possible vlues ` ` ` `# of Y ` ` ` `while` `(next_val <` `=` `B): ` ` ` `if` `(next_val >` `=` `A): ` ` ` `countY ` `+` `=` `1` ` ` `next_val ` `=` `next_val ` `*` `10` `+` `9` ` ` ` ` `return` `(countX ` `*` `countY) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `A ` `=` `1` ` ` `B ` `=` `16` ` ` ` ` `print` `(countPairs(A, B)) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count ` `// all the possible pairs ` `// with X*Y + (X + Y) equal to ` `// number formed by ` `// concatenating X and Y ` `using` `System; ` `class` `GFG{ ` ` ` `// Function for counting pairs ` `static` `int` `countPairs(` `int` `A, ` `int` `B) ` `{ ` ` ` `int` `countY = 0, ` ` ` `countX = (B - A) + 1, ` ` ` `next_val = 9; ` ` ` ` ` `// Count possible vlues ` ` ` `// of Y ` ` ` `while` `(next_val <= B) ` ` ` `{ ` ` ` `if` `(next_val >= A) ` ` ` `{ ` ` ` `countY += 1; ` ` ` `} ` ` ` `next_val = next_val * 10 + 9; ` ` ` `} ` ` ` `return` `(countX * countY); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `A = 1; ` ` ` `int` `B = 16; ` ` ` `Console.Write(countPairs(A, B)); ` `} ` `} ` ` ` `// This code is contributed by Akanksha_Rai ` |

*chevron_right*

*filter_none*

**Output:**

16

**Time Complexity: ** O(log_{10}(B))

**Space Complexity: ** 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:

- Count all distinct pairs with product equal to K
- Count of pairs upto N such whose LCM is not equal to their product for Q queries
- Count of index pairs in array whose range product is a positive integer
- Number of pairs in an array having sum equal to product
- Count of pairs of Array elements which are divisible by K when concatenated
- Count number of ordered pairs with Even and Odd Product
- Count number of triplets with product equal to given number with duplicates allowed
- Count pairs of natural numbers with GCD equal to given number
- Count of pairs (A, B) in range 1 to N such that last digit of A is equal to the first digit of B
- Pairs with GCD equal to one in the given range
- Count of Numbers in Range where first digit is equal to last digit of the number
- Count ordered pairs with product less than N
- Count pairs of numbers from 1 to N with Product divisible by their Sum
- Count unordered pairs (i,j) such that product of a[i] and a[j] is power of two
- Count of all possible pairs having sum of LCM and GCD equal to N
- Count of pairs in an array whose product is a perfect square
- Count pairs with set bits sum equal to K
- Count pairs from two arrays having sum equal to K
- Count of pairs in an array such that the highest power of 2 that divides their product is 1
- Count pairs in Array whose product is a Kth power of any positive integer

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.