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:Â
- 1:1
- 22:22
- 3:33
- 11:1
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++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; Â
// Function to return the count of // identical times the clock shows int countIdentical( int hours, int minutes) { Â
    // To store the count of identical times     // Initialized to 1 because of 0:0     int i, count = 1; Â
    // For single digit hour     for (i = 1; i <= 9 && i < hours; i++) { Â
        // Single digit minute         if (i < minutes)             count++; Â
        // Double digit minutes         if ((i * 10 + i) < minutes)             count++;     } Â
    // For double digit hours     for (i = 11; i <= 99 && i < hours; i = i + 11) { Â
        // Single digit minute         if ((i % 10) < minutes)             count++; Â
        // Double digit minutes         if (i < minutes)             count++;     } Â
    // Return the required count     return count; } Â
// Driver code int main() {     int hours = 24;     int minutes = 60;          // Function Call     cout << countIdentical(hours, minutes); Â
    return 0; } |
Java
// Java implementation of the above approach class GFG { Â
    // Function to return the count of     // identical times the clock shows     static int countIdentical( int hours, int minutes)     { Â
        // To store the count of identical times         // Initialized to 1 because of 0:0         int i, count = 1 ; Â
        // For single digit hour         for (i = 1 ; i <= 9 && i < hours; i++) { Â
            // Single digit minute             if (i < minutes) {                 count++;             } Â
            // Double digit minutes             if ((i * 10 + i) < minutes) {                 count++;             }         } Â
        // For double digit hours         for (i = 11 ; i <= 99 && i < hours; i = i + 11 ) { Â
            // Double digit minutes             if (i < minutes) {                 count++;             } Â
            // Single digit minute             if ((i % 10 ) < minutes) {                 count++;             }         } Â
        // Return the required count         return count;     } Â
    // Driver code     public static void main(String[] args)     {         int hours = 24 ;         int minutes = 60 ;                // Function Call         System.out.println(countIdentical(hours, minutes));     } } Â
/* This code contributed by PrinciRaj1992 */ |
Python3
# Python 3 implementation of the approach Â
# Function to return the count of # identical times the clock shows Â
Â
def countIdentical(hours, minutes): Â
    # To store the count of identical times     # Initialized to 1 because of 0:0     count = 1     i = 1 Â
    # For single digit hour     while (i < = 9 and i < hours): Â
        # Single digit minute         if (i < minutes):             count + = 1 Â
        # Double digit minutes         if ((i * 10 + i) < minutes):             count + = 1 Â
        i + = 1 Â
    # For double digit hours     i = 11     while (i < = 99 and i < hours): Â
         # Double digit minutes         if (i < minutes):             count + = 1 Â
        # Single digit minute         if ((i % 10 ) < minutes):             count + = 1 Â
        i + = 11 Â
    # Return the required count     return count Â
Â
# Driver code if __name__ = = '__main__' :     hours = 24     minutes = 60          # Function Call     print (countIdentical(hours, minutes)) Â
# This code is contributed by # Surendra_Gangwar |
C#
// C# implementation of the above approach using System; Â
class GFG { Â
    // Function to return the count of     // identical times the clock shows     static int countIdentical( int hours, int minutes)     { Â
        // To store the count of identical times         // Initialized to 1 because of 0:0         int i, count = 1; Â
        // For single digit hour         for (i = 1; i <= 9 && i < hours; i++) { Â
            // Single digit minute             if (i < minutes) {                 count++;             } Â
            // Double digit minutes             if ((i * 10 + i) < minutes) {                 count++;             }         } Â
        // For double digit hours         for (i = 11; i <= 99 && i < hours; i = i + 11) { Â
            // Double digit minutes             if (i < minutes) {                 count++;             } Â
            // Single digit minute             if ((i % 10) < minutes) {                 count++;             }         } Â
        // Return the required count         return count;     } Â
    // Driver code     public static void Main(String[] args)     {         int hours = 24;         int minutes = 60;                // Function Call         Console.WriteLine(countIdentical(hours, minutes));     } } Â
// This code has been contributed by 29AjayKumar |
PHP
<?php // PHP implementation of the approach Â
// Function to return the count of // identical times the clock shows function countIdentical( $hours , $minutes ) { Â
    // To store the count of identical times     // Initialized to 1 because of 0:0     $i ;     $count = 1; Â
    // For single digit hour     for ( $i = 1; $i <= 9 && $i < $hours ; $i ++)     { Â
        // Single digit minute         if ( $i < $minutes )             $count ++; Â
        // Double digit minutes         if (( $i * 10 + $i ) < $minutes )             $count ++;     } Â
    // For double digit hours     for ( $i = 11; $i <= 99 &&                   $i < $hours ; $i = $i + 11)     { Â
                 // Double digit minutes         if ( $i < $minutes )             $count ++; Â
        // Single digit minute         if (( $i % 10) < $minutes )             $count ++;     } Â
    // Return the required count     return $count ; } Â
// Driver Code $hours = 24; $minutes = 60; Â
// Function call echo countIdentical( $hours , $minutes ); Â
// This code is contributed by ajit. ?> |
Javascript
<script> Â
// javascript implementation of the above approach   // Function to return the count of // identical times the clock shows     function countIdentical(hours , minutes) { Â
        // To store the count of identical times         // Initialized to 1 because of 0:0         var i, count = 1; Â
        // For single digit hour         for (i = 1; i <= 9 && i < hours; i++) { Â
            // Single digit minute             if (i < minutes) {                 count++;             } Â
            // Double digit minutes             if ((i * 10 + i) < minutes) {                 count++;             }         } Â
        // For var digit hours         for (i = 11; i <= 99 && i < hours; i = i + 11) { Â
            // Double digit minutes             if (i < minutes) {                 count++;             } Â
            // Single digit minute             if ((i % 10) < minutes) {                 count++;             }         } Â
        // Return the required count         return count;     } Â
    // Driver code              var hours = 24;         var minutes = 60; Â
        // Function Call         document.write(countIdentical(hours, minutes)); Â
// This code contributed by Rajput-Ji Â
</script> |
19
Time Complexity: O(1)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!