Skip to content
Related Articles

Related Articles

Count of pairs (A, B) in range 1 to N such that last digit of A is equal to the first digit of B
  • Last Updated : 27 Apr, 2020

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 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:

CPP

filter_none

edit
close

play_arrow

link
brightness_4
code

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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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


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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :