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.
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.
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 the maximum time value is desired, so we decrease the ‘hour’ value from 23 to 0 and similarly ‘minute’ value from 59 to 0.
- After decreasing the ‘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 positions which have ‘?’. Changes at other positions will make time value invalid. To ensure this we will call function isValid().
- Keep on decreasing ‘hour’ and ‘minute’ value until 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:
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.
- Distance between end points of Hour and minute hand at given time
- Distance traveled by Hour and Minute hand in given Time interval
- Time when minute hand and hour hand coincide
- Calculate the angle between hour hand and minute hand
- Find two distinct numbers such that their LCM lies in given range
- Generate a pair of integers from a range [L, R] whose LCM also lies within the range
- Split array into maximum subarrays such that every distinct element lies in a single subarray
- Time taken per hour for stoppage of Car
- How to validate time in 12-hour format using Regular Expression
- How to validate time in 24-hour format using Regular Expression
- Find the GCD that lies in given range
- Choose points from two ranges such that no point lies in both the ranges
- Minimum change in given value so that it lies in all given Ranges
- Check if the given point lies inside given N points of a Convex Polygon
- Count pairs in an array such that the absolute difference between them is ≥ K
- Maximum distance between two elements whose absolute difference is K
- Check whether a given point lies inside a triangle or not
- How to check if a given point lies inside or outside a polygon?
- Check if a given circle lies completely inside the ring formed by two concentric circles
- Count the number of intervals in which a given value lies
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.