Maximize Time by Replacing β€˜_’ in a given 24-Hour Format Time


Maximizing time by replacing β€˜_’ in a given 24-hour format time is a problem that involves calculating the maximum possible time by replacing the missing digits in a given time in a 24-hour format. The task is to find the maximum time possible by replacing the characters β€˜β€™ with any digit. In this tutorial, we will discuss how to solve this problem using the C++ programming language. We will provide a step-by-step explanation of the algorithm used to calculate the maximum possible time, along with the C++ code to implement the algorithm.

Additionally, we will include test examples to illustrate the problem and its solution, as well as an explanation of the test examples. By the end of this tutorial, readers will have a better understanding of how to solve this problem using C++ and will be able to apply this knowledge to solve similar problems in the future. So let’s get started!

Problem Statement

The objective is to determine the maximum possible time by substituting any digit in the positions marked with β€˜_’ in string S, which represents time in a 24-hour format.

Sample Example 1

Input

S = "23:4_"

Output

"23:49"

Explanation βˆ’ In the given input string, the third character is replaced with '9' to obtain the maximum possible time. Therefore, the output is "23:49".

Sample Example 2

Input

S = "1_:22"

Output

"19:22"

Explanation βˆ’ In the given input string, the first character is replaced with '1' to obtain the maximum possible hour in the 24-hour format. Therefore, the output is "19:22".

Note βˆ’ In both examples, the input string S represents a time in a 24-hour format with a missing digit represented by a '_'. The objective is to replace the missing digit with any digit to obtain the maximum possible time. The first example demonstrates how the second digit in the minute's place can be replaced to obtain the maximum time. The second example shows how the first digit in the hour's place can be replaced to obtain the maximum hour in the 24-hour format.

Algorithm

  • Step 1 βˆ’ Take input string S from the user.

  • Step 2 βˆ’ Initialize the maximum hour and minute values to 23 and 59, respectively.

  • Step 3 βˆ’ If the second digit in the minute's place is missing, replace it with '9'.

  • Step 4 βˆ’ If the first digit in the hour's place is missing:

    • If the second digit is less than '4', replace it with '9'.

    • Otherwise, replace it with '3'.

  • Step 5 βˆ’ If the second digit in the hour's place is missing:

    • If the first digit is less than '2' or also missing, replace it with '2'.

    • Otherwise, replace it with '1'.

  • Step 6 βˆ’ Output the maximum possible time.

Example

Following are the programs to implement the above algorithm βˆ’

In the below programs, the missing digits in a given time are replaced to find the maximum possible time. The program checks and replaces the missing second digit in the minute's place with '9'. Then, it handles the missing first digit in the hour's place by replacing it with '2' if the second digit is less than '4', otherwise with '1'. Finally, if the second digit in the hour's place is missing, it replaces it with '3' if the first digit is less than '2', otherwise with '2'. The program outputs the maximum possible time based on the replacements made.

#include <stdio.h>
#include <string.h>

int main() {
   char S[] = "1_:22";
   printf("Given time with missing digits: %s\n", S);
   // Initialize the maximum hour and minute values
   int maxHour = 23;
   int maxMinute = 59;
   // If the second digit in the minute's place is missing, replace it with '9'
   if (S[4] == '_') {
      S[4] = '9';
   }
   // If the first digit in the hour's place is missing, replace it with '2' if the second digit is less than '4',
   // otherwise replace it with '1'
   if (S[1] == '_') {
      if (S[0] < '2') {
         S[1] = '9';
      } else {
         S[1] = '3';
      }
   }
   // If the second digit in the hour's place is missing, replace it with '3' if the first digit is less than '2',
   // otherwise replace it with '2'
   if (S[0] == '_') {
      if (S[1] < '4' || S[1] == '_') {
         S[0] = '2';
      } else {
         S[0] = '1';
      }
   }
   // Output the maximum possible time
   printf("The maximum possible time is: %s\n", S);
   return 0;
}

Output

Given time with missing digits: 1_:22
The maximum possible time is: 19:22
#include <iostream>
#include <algorithm>
int main() {
   std::string S = "1_:22";
   std::cout << "Given time with missing digits: " << S << std::endl;
   // Initialize the maximum hour and minute values
   int maxHour = 23;
   int maxMinute = 59;
   // If the second digit in the minute's place is missing, replace it with '9'
   if (S[4] == '_') {
      S[4] = '9';
   }
   // If the first digit in the hour's place is missing, replace it with '2' if the second digit is less than '4',
   // otherwise replace it with '1'
   if (S[1] == '_') {
      if (S[0] < '2') {
         S[1] = '9';
      } else {
         S[1] = '3';
      }
   }
   // If the second digit in the hour's place is missing, replace it with '3' if the first digit is less than '2',
   // otherwise replace it with '2'
   if (S[0] == '_') {
      if (S[1] < '4' || S[1] == '_') {
         S[0] = '2';
      } else {
         S[0] = '1';
      }
   }
   // Output the maximum possible time
   std::cout << "The maximum possible time is: " << S << std::endl;
   return 0;
}

Output

Given time with missing digits: 1_:22
The maximum possible time is: 19:22
public class MaximumTime {
   public static String calculateMaximumTime(String S) {
      // Initialize the maximum hour and minute values
      int maxHour = 23;
      int maxMinute = 59;

      char[] chars = S.toCharArray();

      // If the second digit in the minute's place is missing, replace it with '9'
      if (chars[4] == '_') {
         chars[4] = '9';
      }

      // If the first digit in the hour's place is missing, replace it with '2' if the second digit is less than '4',
      // otherwise replace it with '1'
      if (chars[1] == '_') {
         if (chars[0] < '2') {
            chars[1] = '9';
         } else {
            chars[1] = '3';
         }
      }

      // If the second digit in the hour's place is missing, replace it with '3' if the first digit is less than '2',
      // otherwise replace it with '2'
      if (chars[0] == '_') {
         if (chars[1] < '4' || chars[1] == '_') {
            chars[0] = '2';
         } else {
            chars[0] = '1';
         }
      }

      // Convert the character array back to a string
      return new String(chars);
   }

   public static void main(String[] args) {
      String inputStr = "1_:22";
      System.out.println("Given time with missing digits: " + inputStr);
      String result = calculateMaximumTime(inputStr);
      System.out.println("The maximum possible time is: " + result);
   }
}

Output

Given time with missing digits: 1_:22
The maximum possible time is: 19:22
def calculate_maximum_time(S):
   # Initialize the maximum hour and minute values
   max_hour = 23
   max_minute = 59
    
   # If the second digit in the minute's place is missing, replace it with '9'
   S = list(S)
   if S[4] == '_':
      S[4] = '9'
    
   # If the first digit in the hour's place is missing, replace it with '2' if the second digit is less than '4',
   # otherwise replace it with '1'
   if S[1] == '_':
      if S[0] < '2':
         S[1] = '9'
      else:
         S[1] = '3'
    
   # If the second digit in the hour's place is missing, replace it with '3' if the first digit is less than '2',
   # otherwise replace it with '2'
   if S[0] == '_':
      if S[1] < '4' or S[1] == '_':
         S[0] = '2'
      else:
         S[0] = '1'
    
   # Convert the list back to a string
   result = ''.join(S)
   return result

input_str = "1_:22"
print("Given time with missing digits:", input_str)
result = calculate_maximum_time(input_str)
print("The maximum possible time is:", result)

Output

Given time with missing digits: 1_:22
The maximum possible time is: 19:22

Conclusion

To sum up, the problem of maximizing time by replacing '_' in a given 24-hour format time can be easily solved using the algorithm and C++ code provided in this tutorial. By following the step-by-step approach and understanding the logic behind the algorithm, readers can gain valuable insights into solving similar problems. With the help of the test examples, readers can verify the correctness of their code and gain confidence in their programming skills.

Updated on: 23-Oct-2023

76 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements