Count of pairs (A, B) in range 1 to N such that last digit of A is equal to the first digit of B

Given a number N, the task is to find the number of pairs (A, B) in range [1, N] such that the last digit of A is equal to the first digit of B, and the first digit of A is equal to the last digit of B.

Examples:

Input: N = 25
Output: 17
Explanation:
The pairs are:
(1, 1), (1, 11), (2, 2), (2, 22), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9), (11, 1), (11, 11), (12, 21), (21, 12), (22, 2), (22, 22)

Input: N = 100
Output: 108

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: For each pair of integers (i, j)(0 ≤ i, j ≤ 9), let us define ci, j (1 ≤ k ≤ N) which is the count of first digit of k is equal to i, and the last digit is equal to j. By using ci, j, the answer for the problem can be calculated by i=09j=09 ci, j * cj, i.

Below is the implementation of the above approach:

 `// C++ program to implement the above approach ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to Count of pairs (A, B) in range 1 to N ` `int` `pairs(``int` `n) ` `{ ` `    ``vector > c(10, vector<``int``>(10, 0)); ` ` `  `    ``int` `tmp = 1; ` ` `  `    ``// count C i, j ` `    ``for` `(``int` `i = 1; i <= n; i++) { ` `        ``if` `(i >= tmp * 10) ` `            ``tmp *= 10; ` `        ``c[i / tmp][i % 10]++; ` `    ``} ` ` `  `    ``// Calculate number of pairs ` `    ``long` `long` `ans = 0; ` `    ``for` `(``int` `i = 1; i < 10; i++) ` `        ``for` `(``int` `j = 1; j < 10; j++) ` `            ``ans += (``long` `long``)c[i][j] * c[j][i]; ` ` `  `    ``return` `ans; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 25; ` ` `  `    ``// Function call ` `    ``cout << pairs(n); ` ` `  `    ``return` `0; ` `} `

 `// Java program to implement the above approach ` ` `  `class` `GFG{ ` `  `  `// Function to Count of pairs (A, B) in range 1 to N ` `static` `int` `pairs(``int` `n) ` `{ ` `    ``int` `[][]c = ``new` `int``[``10``][``10``]; ` `  `  `    ``int` `tmp = ``1``; ` `  `  `    ``// count C i, j ` `    ``for` `(``int` `i = ``1``; i <= n; i++) { ` `        ``if` `(i >= tmp * ``10``) ` `            ``tmp *= ``10``; ` `        ``c[i / tmp][i % ``10``]++; ` `    ``} ` `  `  `    ``// Calculate number of pairs ` `    ``int` `ans = ``0``; ` `    ``for` `(``int` `i = ``1``; i < ``10``; i++) ` `        ``for` `(``int` `j = ``1``; j < ``10``; j++) ` `            ``ans += c[i][j] * c[j][i]; ` `  `  `    ``return` `ans; ` `} ` `  `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `n = ``25``; ` `  `  `    ``// Function call ` `    ``System.out.print(pairs(n)); ` `  `  `} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

 `# Python3 program to implement the above approach ` ` `  `# Function to Count of pairs (A, B) in range 1 to N ` `def` `pairs(n): ` `    ``c ``=` `[[``0` `for` `i ``in` `range``(``10``)] ``for` `i ``in` `range``(``10``)] ` ` `  `    ``tmp ``=` `1` ` `  `    ``# count C i, j ` `    ``for` `i ``in` `range``(``1``, n ``+` `1``): ` `        ``if` `(i >``=` `tmp ``*` `10``): ` `            ``tmp ``*``=` `10` `        ``c[i ``/``/` `tmp][i ``%` `10``] ``+``=` `1` ` `  `    ``# Calculate number of pairs ` `    ``ans ``=` `0` `    ``for` `i ``in` `range``(``1``, ``10``): ` `        ``for` `j ``in` `range``(``1``, ``10``): ` `            ``ans ``+``=` `c[i][j] ``*` `c[j][i] ` ` `  `    ``return` `ans ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``n ``=` `25` ` `  `    ``# Function call ` `    ``print``(pairs(n)) ` ` `  `# This code is contributed by mohit kumar 29     `

 `// C# program to implement the above approach ` `using` `System; ` ` `  `class` `GFG{ ` `   `  `// Function to Count of pairs (A, B) in range 1 to N ` `static` `int` `pairs(``int` `n) ` `{ ` `    ``int` `[,]c = ``new` `int``[10, 10]; ` `   `  `    ``int` `tmp = 1; ` `   `  `    ``// count C i, j ` `    ``for` `(``int` `i = 1; i <= n; i++) { ` `        ``if` `(i >= tmp * 10) ` `            ``tmp *= 10; ` `        ``c[i / tmp, i % 10]++; ` `    ``} ` `   `  `    ``// Calculate number of pairs ` `    ``int` `ans = 0; ` `    ``for` `(``int` `i = 1; i < 10; i++) ` `        ``for` `(``int` `j = 1; j < 10; j++) ` `            ``ans += c[i, j] * c[j, i]; ` `   `  `    ``return` `ans; ` `} ` `   `  `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``int` `n = 25; ` `   `  `    ``// Function call ` `    ``Console.Write(pairs(n)); ` `   `  `} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

Output:
```17
```

Time Complexity: O(N)

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.

pawanasipugmailcom

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.

Improved By : mohit kumar 29, Rajput-Ji

Article Tags :
Practice Tags :