Count numbers with same first and last digits
Given an interval, the task is to count numbers that have the same first and last digits. For example, 1231 has the same first and last digits.
Examples:
Input : start = 7, end : 68
Output : 9
Number with same first and last digits are,
7 8 9 11 22 33 44 55 66.
Input : start = 5, end : 40
Output : 8
Let us first consider the below examples to understand the approach.
From 120 to 130, only 121 has same starting and ending digit
From 440 to 450, only 444 has same starting and ending digit
From 1050 to 1060, only 1051 has same starting and ending digit
From the above examples, we can observe that in each ten number span we have only one number which has the given property except 1 to 10 which has 9 numbers (all one-digit numbers) having the same starting and ending digit.
Using above property we can solve given problem, first we break the given interval into two parts that is if interval is l to r, we break this into 1 to l and 1 to r, our answer is obtained by subtracting result of first query from second query.
Now we remain to find the count of numbers with given property in range 1 to x, for this, we divide x by 10, which gives the number of 10-spans. We add 9 to the span for taking one-digit numbers into account. If the last digit of x is smaller than the first digit of x, then 1 should be decreased in the final answer to discard the last ten span number because that number is out of range.
C++
#include <bits/stdc++.h>
using namespace std;
int getFirstDigit( int x)
{
while (x >= 10)
x /= 10;
return x;
}
int getCountWithSameStartAndEndFrom1( int x)
{
if (x < 10)
return x;
int tens = x / 10;
int res = tens + 9;
int firstDigit = getFirstDigit(x);
int lastDigit = x % 10;
if (lastDigit < firstDigit)
res--;
return res;
}
int getCountWithSameStartAndEnd( int start, int end)
{
return getCountWithSameStartAndEndFrom1(end)
- getCountWithSameStartAndEndFrom1(start - 1);
}
int main()
{
int start = 5, end = 40;
cout << getCountWithSameStartAndEnd(start, end);
return 0;
}
|
C
#include <stdio.h>
int getFirstDigit( int x)
{
while (x >= 10)
x /= 10;
return x;
}
int getCountWithSameStartAndEndFrom1( int x)
{
if (x < 10)
return x;
int tens = x / 10;
int res = tens + 9;
int firstDigit = getFirstDigit(x);
int lastDigit = x % 10;
if (lastDigit < firstDigit)
res--;
return res;
}
int getCountWithSameStartAndEnd( int start, int end)
{
return getCountWithSameStartAndEndFrom1(end)
- getCountWithSameStartAndEndFrom1(start - 1);
}
int main()
{
int start = 5, end = 40;
printf ( "%d" ,getCountWithSameStartAndEnd(start, end));
return 0;
}
|
Java
import java.util.*;
class Digits {
public static int getFirstDigit( int x)
{
while (x >= 10 )
x /= 10 ;
return x;
}
public static int getCountWithSameStartAndEndFrom1( int x)
{
if (x < 10 )
return x;
int tens = x / 10 ;
int res = tens + 9 ;
int firstDigit = getFirstDigit(x);
int lastDigit = x % 10 ;
if (lastDigit < firstDigit)
res--;
return res;
}
public static int getCountWithSameStartAndEnd( int start,
int end)
{
return getCountWithSameStartAndEndFrom1(end)
- getCountWithSameStartAndEndFrom1(start - 1 );
}
public static void main(String[] args)
{
int start = 5 , end = 40 ;
System.out.print(getCountWithSameStartAndEnd(start,
end));
}
}
|
Python3
def getFirstDigit(x) :
while (x > = 10 ) :
x / / = 10
return x
def getCountWithSameStartAndEndFrom1(x) :
if (x < 10 ):
return x
tens = x / / 10
res = tens + 9
firstDigit = getFirstDigit(x)
lastDigit = x % 10
if (lastDigit < firstDigit) :
res = res - 1
return res
def getCountWithSameStartAndEnd(start, end) :
return (getCountWithSameStartAndEndFrom1(end) -
getCountWithSameStartAndEndFrom1(start - 1 ))
start = 5
end = 40
print (getCountWithSameStartAndEnd(start, end))
|
C#
using System;
class GFG {
public static int getFirstDigit( int x)
{
while (x >= 10)
x /= 10;
return x;
}
public static int getCountWithSameStartAndEndFrom1(
int x)
{
if (x < 10)
return x;
int tens = x / 10;
int res = tens + 9;
int firstDigit = getFirstDigit(x);
int lastDigit = x % 10;
if (lastDigit < firstDigit)
res--;
return res;
}
public static int getCountWithSameStartAndEnd(
int start, int end)
{
return getCountWithSameStartAndEndFrom1(end)
- getCountWithSameStartAndEndFrom1(start - 1);
}
public static void Main()
{
int start = 5, end = 40;
Console.Write(getCountWithSameStartAndEnd(start,
end));
}
}
|
PHP
<?php
function getFirstDigit( $x )
{
while ( $x >= 10)
$x /= 10;
return $x ;
}
function getCountWithSameStartAndEndFrom1( $x )
{
if ( $x < 10)
return $x ;
$tens = $x / 10;
$res = $tens + 9;
$firstDigit = getFirstDigit( $x );
$lastDigit = $x % 10;
if ( $lastDigit < $firstDigit )
$res --;
return $res ;
}
function getCountWithSameStartAndEnd( $start , $end )
{
return getCountWithSameStartAndEndFrom1( $end )
- getCountWithSameStartAndEndFrom1( $start - 1);
}
$start = 5;
$end = 40;
echo getCountWithSameStartAndEnd( $start , $end );
?>
|
Javascript
<script>
function getFirstDigit(x)
{
while (x >= 10)
x /= 10;
return x;
}
function getCountWithSameStartAndEndFrom1(x)
{
if (x < 10)
return x;
let tens = x / 10;
let res = tens + 9;
let firstDigit = getFirstDigit(x);
let lastDigit = x % 10;
if (lastDigit < firstDigit)
res--;
return res;
}
function getCountWithSameStartAndEnd(start,
end)
{
return getCountWithSameStartAndEndFrom1(end)
- getCountWithSameStartAndEndFrom1(start - 1);
}
let start = 5, end = 40;
document.write(getCountWithSameStartAndEnd(start,
end));
</script>
|
Output:
8
Time Complexity: O(log10N)
Auxiliary Space: O(1)
Last Updated :
26 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...