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, 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:
Time Complexity: O(1)
Auxillary 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
- Time taken per hour for stoppage of Car
- Find the GCD that lies in given range
- How to validate time in 12-hour format using Regular Expression
- How to validate time in 24-hour format using Regular Expression
- Maximum difference of zeros and ones in binary string | Set 2 (O(n) time)
- Find Maximum and Minimum of two numbers using Absolute function
- Absolute difference between the first X and last X Digits of N
- Pair with min absolute difference and whose product is N+1 or N+2
- Minimum absolute difference between N and any power of 2
- Absolute difference between set and unset bit count in N
- Minimize the absolute difference of sum of two subsets
- Minimum absolute difference between N and a power of 2
- Closest Palindrome Number (absolute difference Is min)
- Count pairs in an array such that the absolute difference between them is ≥ K
- Sum of absolute difference of all pairs raised to power K
- Maximize product of absolute index difference with K
Improved By : mohit kumar 29