Count how many times the given digital clock shows identical digits
Given a generic digital clock, having h number of hours and m number of minutes, the task is to find how many times the clock shows identical time. A specific time is said to be identical if every digit in the hours and minutes is same i.e. the time is of type D:D, D:DD, DD:D or DD:DD.
Note that the time is written on the digital clock without any leading zeros and the clock shows time between 0 to h – 1 hours and 0 to m – 1 minutes. Few examples of identical times are:
Examples:
Input: hours = 24, minutes = 60
Output: 19
The clock has 24 hours and 60 minutes.
So the identical times will be:
Single digit hours and single digit minutes -> 0:0, 1:1, 2:2, …., 9:9
Single digit hours and double digit minutes -> 1:11, 2:22, 3:33, 4:44 and 5:55
Double digit hours and single digit minutes -> 11:1 and 22:2
Double digit hours and double digit minutes -> 11:11, 22:22
Total = 10 + 5 + 2 + 2 = 19
Input: hours = 34, minutes = 50
Output: 20
Approach: As we can see in the explained example, we have to first count the single-digit (of hours) identical times and then double-digit hours. During each of these counts, we need to consider single-digit minutes as well as double-digit minutes.
There will be two loops. First loop deals with single-digit hours. And the second deals with double-digit hours. In each of the loops, there should be two conditions. First, if the iterator variable is less than total minutes, then increment the counter. Second, if (iterator variable + iterator variable * 10) is less than total minutes, increment the counter. In the end, we will have the total identical times that clock shows.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countIdentical( int hours, int minutes)
{
int i, count = 1;
for (i = 1; i <= 9 && i < hours; i++) {
if (i < minutes)
count++;
if ((i * 10 + i) < minutes)
count++;
}
for (i = 11; i <= 99 && i < hours; i = i + 11) {
if ((i % 10) < minutes)
count++;
if (i < minutes)
count++;
}
return count;
}
int main()
{
int hours = 24;
int minutes = 60;
cout << countIdentical(hours, minutes);
return 0;
}
|
Java
class GFG {
static int countIdentical( int hours, int minutes)
{
int i, count = 1 ;
for (i = 1 ; i <= 9 && i < hours; i++) {
if (i < minutes) {
count++;
}
if ((i * 10 + i) < minutes) {
count++;
}
}
for (i = 11 ; i <= 99 && i < hours; i = i + 11 ) {
if (i < minutes) {
count++;
}
if ((i % 10 ) < minutes) {
count++;
}
}
return count;
}
public static void main(String[] args)
{
int hours = 24 ;
int minutes = 60 ;
System.out.println(countIdentical(hours, minutes));
}
}
|
Python3
def countIdentical(hours, minutes):
count = 1
i = 1
while (i < = 9 and i < hours):
if (i < minutes):
count + = 1
if ((i * 10 + i) < minutes):
count + = 1
i + = 1
i = 11
while (i < = 99 and i < hours):
if (i < minutes):
count + = 1
if ((i % 10 ) < minutes):
count + = 1
i + = 11
return count
if __name__ = = '__main__' :
hours = 24
minutes = 60
print (countIdentical(hours, minutes))
|
C#
using System;
class GFG {
static int countIdentical( int hours, int minutes)
{
int i, count = 1;
for (i = 1; i <= 9 && i < hours; i++) {
if (i < minutes) {
count++;
}
if ((i * 10 + i) < minutes) {
count++;
}
}
for (i = 11; i <= 99 && i < hours; i = i + 11) {
if (i < minutes) {
count++;
}
if ((i % 10) < minutes) {
count++;
}
}
return count;
}
public static void Main(String[] args)
{
int hours = 24;
int minutes = 60;
Console.WriteLine(countIdentical(hours, minutes));
}
}
|
PHP
<?php
function countIdentical( $hours , $minutes )
{
$i ;
$count = 1;
for ( $i = 1; $i <= 9 && $i < $hours ; $i ++)
{
if ( $i < $minutes )
$count ++;
if (( $i * 10 + $i ) < $minutes )
$count ++;
}
for ( $i = 11; $i <= 99 &&
$i < $hours ; $i = $i + 11)
{
if ( $i < $minutes )
$count ++;
if (( $i % 10) < $minutes )
$count ++;
}
return $count ;
}
$hours = 24;
$minutes = 60;
echo countIdentical( $hours , $minutes );
?>
|
Javascript
<script>
function countIdentical(hours , minutes) {
var i, count = 1;
for (i = 1; i <= 9 && i < hours; i++) {
if (i < minutes) {
count++;
}
if ((i * 10 + i) < minutes) {
count++;
}
}
for (i = 11; i <= 99 && i < hours; i = i + 11) {
if (i < minutes) {
count++;
}
if ((i % 10) < minutes) {
count++;
}
}
return count;
}
var hours = 24;
var minutes = 60;
document.write(countIdentical(hours, minutes));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
02 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...