Given a **24-hour time** value, where on some numbers are question marks( ‘?’ ), and two integers **L** and **R**. The question mark can be replaced with any number. The task is to find the maximum time such that the absolute difference between hour value and minute value lies in the interval [**L, R**]. If no possible value of time exists, then print **-1**.

**Examples:**

Input :time = “2?:03”, L = 5, R = 18

Output :21:03

Explanation :Since, the difference is 21 – 3 = 18 and the time value 21:03 is the largest possible value whose difference lies in the range [5, 18]

Input :time = “??:??”, L = 60, R = 69

Output :-1

Explanation :Since maximum possible difference between hour value and minute value is 59. So, no time value is possible.

**Approach :**

We will run two nested loops, one to represent ‘hour’ value from 23 to 0 and another to represent ‘minute’ value from 59 to 0. We keep decreasing ‘hour’ and ‘minute’ value until we get the desired time value.

- Since, maximum time value is desired, so we decrease ‘hour’ value from 23 to 0 and similarly ‘minute’ value from 59 to 0.
- After decreasing ‘hour’ and ‘minute’ value, we need to check if the ‘hour’ and ‘minute’ value is valid or not.
- Change in value of ‘hour’ and ‘minute’ is allowed only at those position which have ‘?’. Changes at other postion will make time value invalid. To ensure this we will call function isValid().
- Keep on decreasing ‘hour’ and ‘minute’ value untill a valid time is found whose difference lies in the range [
**L, R**]. - If no valid time is found, then print “-1”.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function checks whether ` `// given time is correct ` `bool` `isValid(` `char` `a1, ` `char` `a2, ` ` ` `string str, ` `int` `flag) ` `{ ` ` ` ` ` `char` `v1, v2; ` ` ` `// To check minute value of time ` ` ` `if` `(flag == 0) { ` ` ` `v1 = str[4]; ` ` ` `v2 = str[3]; ` ` ` `} ` ` ` `else` `{ ` ` ` `// To check hour value of time ` ` ` `v1 = str[1]; ` ` ` `v2 = str[0]; ` ` ` `} ` ` ` ` ` `// Changes in value is not allowed ` ` ` `// at position where '?' is not ` ` ` `// present ` ` ` `if` `(v1 != a1 && v1 != ` `'?'` `) ` ` ` `return` `false` `; ` ` ` `if` `(v2 != a2 && v2 != ` `'?'` `) ` ` ` `return` `false` `; ` ` ` ` ` `return` `true` `; ` `} ` ` ` `// Function checks whether ` `// the absolute difference ` `// between hour and minute ` `// value is within [L, R] ` `bool` `inRange(` `int` `hh, ` ` ` `int` `mm, ` `int` `L, ` `int` `R) ` `{ ` ` ` `int` `a = ` `abs` `(hh - mm); ` ` ` ` ` `// Checks if the difference is outside ` ` ` `// the give range ` ` ` `if` `(a < L || a > R) ` ` ` `return` `false` `; ` ` ` ` ` `return` `true` `; ` `} ` ` ` `// Displays time in proper ` `// 24-hour format ` `void` `displayTime(` `int` `hh, ` `int` `mm) ` `{ ` ` ` `if` `(hh > 10) ` ` ` `cout << hh << ` `":"` `; ` ` ` `else` `if` `(hh < 10) ` ` ` `cout << ` `"0"` `<< hh << ` `":"` `; ` ` ` ` ` `if` `(mm > 10) ` ` ` `cout << mm << endl; ` ` ` `else` `if` `(mm < 10) ` ` ` `cout << ` `"0"` `<< mm << endl; ` `} ` ` ` `// Function find the desired ` `// value of time whose difference ` `// lies in the range [L, R] ` `void` `maximumTimeWithDifferenceInRange( ` ` ` `string str, ` ` ` `int` `L, ` `int` `R) ` `{ ` ` ` `int` `i, j; ` ` ` `int` `h1, h2, m1, m2; ` ` ` ` ` `// Decrease hour value from 23 to 0 ` ` ` `for` `(i = 23; i >= 0; i--) { ` ` ` `h1 = i % 10; ` ` ` `h2 = i / 10; ` ` ` ` ` `// Check if the hour value is valid ` ` ` `// if not valid then no need to change ` ` ` `// minute value, since time will still ` ` ` `// remain in valid, to check hour value ` ` ` `// flag is set to 1. ` ` ` `if` `(!isValid(h1 + ` `'0'` `, h2 + ` `'0'` `, str, 1)) { ` ` ` `continue` `; ` ` ` `} ` ` ` ` ` `// Decrease minute value from 59 to 0 ` ` ` `for` `(j = 59; j >= 0; j--) { ` ` ` `m1 = j % 10; ` ` ` `m2 = j / 10; ` ` ` ` ` `// Check if the minute value is valid, ` ` ` `// if not valid then skip the current ` ` ` `// iteration, to check 'minute' value ` ` ` `// flag is set to 0. ` ` ` `if` `(!isValid(m1 + ` `'0'` `, m2 + ` `'0'` `, str, 0)) { ` ` ` `continue` `; ` ` ` `} ` ` ` ` ` `if` `(inRange(i, j, L, R)) { ` ` ` `displayTime(i, j); ` ` ` `return` `; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `if` `(inRange(i, j, L, R)) ` ` ` `displayTime(i, j); ` ` ` `else` ` ` `cout << ` `"-1"` `<< endl; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// Input time ` ` ` `string timeValue = ` `"??:??"` `; ` ` ` ` ` `// Difference range ` ` ` `int` `L = 20, R = 39; ` ` ` `maximumTimeWithDifferenceInRange( ` ` ` `timeValue, ` ` ` `L, R); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach ` ` ` `# Function checks whether ` `# given time is correct ` `def` `isValid(a1, a2, strr, flag): ` ` ` ` ` `v1, v2 ` `=` `0` `, ` `0` ` ` ` ` `# To check minute value of time ` ` ` `if` `(flag ` `=` `=` `0` `): ` ` ` `v1 ` `=` `strr[` `4` `] ` ` ` `v2 ` `=` `strr[` `3` `] ` ` ` `else` `: ` ` ` ` ` `# To check hour value of time ` ` ` `v1 ` `=` `strr[` `1` `] ` ` ` `v2 ` `=` `strr[` `0` `] ` ` ` ` ` `# Changes in value is not allowed ` ` ` `# at position where '?' is not ` ` ` `# present ` ` ` `if` `(v1 !` `=` `a1 ` `and` `v1 !` `=` `'?'` `): ` ` ` `return` `False` ` ` `if` `(v2 !` `=` `a2 ` `and` `v2 !` `=` `'?'` `): ` ` ` `return` `False` ` ` ` ` `return` `True` ` ` `# Function checks whether ` `# the absolute difference ` `# between hour and minute ` `# value is within [L, R] ` `def` `inRange(hh, mm, L, R): ` ` ` `a ` `=` `abs` `(hh ` `-` `mm) ` ` ` ` ` `# Checks if the difference is ` ` ` `# outside the give range ` ` ` `if` `(a < L ` `or` `a > R): ` ` ` `return` `False` ` ` ` ` `return` `True` ` ` `# Displays time in proper ` `# 24-hour format ` `def` `displayTime(hh, mm): ` ` ` ` ` `if` `(hh > ` `10` `): ` ` ` `print` `(hh, end ` `=` `":"` `) ` ` ` `elif` `(hh < ` `10` `): ` ` ` `print` `(` `"0"` `, hh, end ` `=` `":"` `) ` ` ` ` ` `if` `(mm > ` `10` `): ` ` ` `print` `(mm) ` ` ` `elif` `(mm < ` `10` `): ` ` ` `print` `(` `"0"` `, mm) ` ` ` `# Function find the desired ` `# value of time whose difference ` `# lies in the range [L, R] ` `def` `maximumTimeWithDifferenceInRange(strr, L, R): ` ` ` ` ` `i, j ` `=` `0` `, ` `0` ` ` `h1, h2, m1, m2 ` `=` `0` `, ` `0` `, ` `0` `, ` `0` ` ` ` ` `# Decrease hour value from 23 to 0 ` ` ` `for` `i ` `in` `range` `(` `23` `, ` `-` `1` `, ` `-` `1` `): ` ` ` `h1 ` `=` `i ` `%` `10` ` ` `h2 ` `=` `i ` `/` `/` `10` ` ` ` ` `# Check if the hour value is valid ` ` ` `# if not valid then no need to change ` ` ` `# minute value, since time will still ` ` ` `# remain in valid, to check hour value ` ` ` `# flag is set to 1. ` ` ` `if` `(` `not` `isValid(` `chr` `(h1), ` `chr` `(h2), strr, ` `1` `)): ` ` ` `continue` ` ` ` ` `# Decrease minute value from 59 to 0 ` ` ` `for` `j ` `in` `range` `(` `59` `, ` `-` `1` `, ` `-` `1` `): ` ` ` `m1 ` `=` `j ` `%` `10` ` ` `m2 ` `=` `j ` `/` `/` `10` ` ` ` ` `# Check if the minute value is valid, ` ` ` `# if not valid then skip the current ` ` ` `# iteration, to check 'minute' value ` ` ` `# flag is set to 0. ` ` ` `if` `(` `not` `isValid(` `chr` `(m1), ` `chr` `(m2), ` ` ` `strr, ` `0` `)): ` ` ` `continue` ` ` ` ` `if` `(inRange(i, j, L, R)): ` ` ` `displayTime(i, j) ` ` ` `return` ` ` ` ` `if` `(inRange(i, j, L, R)): ` ` ` `displayTime(i, j) ` ` ` `else` `: ` ` ` `print` `(` `-` `1` `) ` ` ` `# Driver code ` ` ` `# Input time ` `timeValue ` `=` `"??:??"` ` ` `# Difference range ` `L ` `=` `20` `R ` `=` `39` ` ` `maximumTimeWithDifferenceInRange(timeValue, L, R) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

**Output:**

23:59

**Time Complexity: ** *O(1)*

**Auxiliary Space: ** *O(1)*

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Distance between end points of Hour and minute hand at given time
- How to validate time in 12-hour format using Regular Expression
- How to validate time in 24-hour format using Regular Expression
- Find the time which is palindromic and comes after the given time
- Maximum difference of zeros and ones in binary string | Set 2 (O(n) time)
- Sum of minimum and the maximum difference between two given Strings
- Closest Palindrome Number (absolute difference Is min)
- Maximum length palindromic substring such that it starts and ends with given char
- Count numbers in range such that digits in it and it's product with q are unequal
- Queries to print the character that occurs the maximum number of times in a given range
- Number of ways in which the substring in range [L, R] can be formed using characters out of the range
- Maximum splits in binary string such that each substring is divisible by given odd number
- Minimum replacements such that the difference between the index of the same characters is divisible by 3
- Maximum length palindrome that can be created with characters in range L and R
- Count number of indices such that s[i] = s[i+1] : Range queries
- Sum of all LCP of maximum length by selecting any two Strings at a time
- Queries for rotation and Kth character of the given string in constant time
- Arrange a binary string to get maximum value within a range of indices
- Find the number in a range having maximum product of the digits
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.