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

**Approach:** For each pair of integers **(i, j)(0 ≤ i, j ≤ 9)**, let us define **c _{i, 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

**c**, the answer for the problem can be calculated by

_{i, j}**∑**.

_{i=0}^{9}∑_{j=0}^{9}c_{i, j}* c_{j, i}Below is the implementation of the above approach:

## CPP

`// C++ program to implement the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to Count of pairs (A, B) in range 1 to N ` `int` `pairs(` `int` `n) ` `{ ` ` ` `vector<vector<` `int` `> > 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; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// 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 ` |

*chevron_right*

*filter_none*

## Python3

`# 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 ` |

*chevron_right*

*filter_none*

## C#

`// 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 ` |

*chevron_right*

*filter_none*

**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.