Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Forming the nearest time using current time in JavaScript
In JavaScript, finding the nearest time using current time digits is a common algorithmic problem. We need to form the next closest time by reusing only the available digits from the input time.
Problem Statement
We are required to write a JavaScript function that takes in a string representing time in "HH:MM" format. The function should form the next closest time by reusing only the current digits, with no limit on how many times a digit can be reused.
Example Input and Output
For the input time '19:34':
Input: '19:34' Available digits: 1, 9, 3, 4 Output: '19:39'
The next closest time using digits 1, 9, 3, 4 is 19:39, which occurs 5 minutes later. Note that 19:33 would occur 23 hours and 59 minutes later, making it farther in the future.
Algorithm Approach
The solution works by checking each position from right to left (minutes ? hours) to find the next valid time:
- Extract all available digits and sort them
- Try to increment the rightmost minute digit first
- If not possible, try the leftmost minute digit (must be ? 5)
- If not possible, try the rightmost hour digit
- If not possible, try the leftmost hour digit (must be ? 2)
- If no increment is possible, wrap to the smallest possible time
Implementation
const time = '19:34';
const findClosestTime = (time = '') => {
const [a, b, c, d] = [time[0], time[1], time[3], time[4]].map(x => Number(x));
const sorted = [a, b, c, d].sort((x, y) => x - y);
const min = Math.min(a, b, c, d);
// Try to increment rightmost minute digit (d)
const d2 = sorted.find(x => x > d);
if (d2 !== undefined) {
return `${a}${b}:${c}${d2}`;
}
// Try to increment leftmost minute digit (c), must be <= 5
const c2 = sorted.find(x => x > c && x <= 5);
if (c2 !== undefined) {
return `${a}${b}:${c2}${min}`;
}
// Try to increment rightmost hour digit (b)
const b2 = sorted.find(x => x > b && a * 10 + x <= 23);
if (b2 !== undefined) {
return `${a}${b2}:${min}${min}`;
}
// Try to increment leftmost hour digit (a), must be <= 2
const a2 = sorted.find(x => x > a && x <= 2);
if (a2 !== undefined) {
return `${a2}${min}:${min}${min}`;
}
// Wrap to next day with smallest digits
return `${min}${min}:${min}${min}`;
};
console.log(findClosestTime(time));
19:39
How It Works
The function extracts digits [1, 9, 3, 4] from '19:34' and sorts them to [1, 3, 4, 9]. It then checks:
- Minutes (second digit): Can we find a digit > 4? Yes, 9. Result: 19:39
- Since we found a valid increment, we return immediately without checking other positions
Edge Cases
The algorithm handles various edge cases:
- Time constraints: Hours must be ? 23, minutes ? 59
- Wrap around: When no increment is possible, it returns the earliest possible time using available digits
- Digit reuse: Same digits can be used multiple times
Conclusion
This solution efficiently finds the nearest future time by systematically checking each digit position from right to left. The time complexity is O(1) since we work with a fixed number of digits, making it optimal for this problem.
