Given a number N, the task is to find the number of pairs (A, B) in the 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 the 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=09 ?j=09 ci, j * cj, i .
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
using namespace std;
int pairs( int n)
{
vector<vector< int > > c(10, vector< int >(10, 0));
int tmp = 1;
for ( int i = 1; i <= n; i++) {
if (i >= tmp * 10)
tmp *= 10;
c[i / tmp][i % 10]++;
}
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;
}
int main()
{
int n = 25;
cout << pairs(n);
return 0;
}
|
Java
class GFG{
static int pairs( int n)
{
int [][]c = new int [ 10 ][ 10 ];
int tmp = 1 ;
for ( int i = 1 ; i <= n; i++) {
if (i >= tmp * 10 )
tmp *= 10 ;
c[i / tmp][i % 10 ]++;
}
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;
}
public static void main(String[] args)
{
int n = 25 ;
System.out.print(pairs(n));
}
}
|
Python3
def pairs(n):
c = [[ 0 for i in range ( 10 )] for i in range ( 10 )]
tmp = 1
for i in range ( 1 , n + 1 ):
if (i > = tmp * 10 ):
tmp * = 10
c[i / / tmp][i % 10 ] + = 1
ans = 0
for i in range ( 1 , 10 ):
for j in range ( 1 , 10 ):
ans + = c[i][j] * c[j][i]
return ans
if __name__ = = '__main__' :
n = 25
print (pairs(n))
|
C#
using System;
class GFG{
static int pairs( int n)
{
int [,]c = new int [10, 10];
int tmp = 1;
for ( int i = 1; i <= n; i++) {
if (i >= tmp * 10)
tmp *= 10;
c[i / tmp, i % 10]++;
}
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;
}
public static void Main(String[] args)
{
int n = 25;
Console.Write(pairs(n));
}
}
|
Javascript
<script>
function pairs(n)
{
let c = new Array(10);
for ( var i = 0; i < c.length; i++) {
c[i] = new Array(2);
}
for ( var i = 0; i < c.length; i++) {
for ( var j = 0; j < c.length; j++) {
c[i][j] = 0;
}
}
let tmp = 1;
for (let i = 1; i <= n; i++) {
if (i >= tmp * 10)
tmp *= 10;
c[(Math.floor(i / tmp))][i % 10]++;
}
let ans = 0;
for (let i = 1; i < 10; i++)
for (let j = 1; j < 10; j++)
ans += c[i][j] * c[j][i];
return ans;
}
let n = 25;
document.write(pairs(n));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(10*10)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
24 Mar, 2023
Like Article
Save Article