Binary Watch - Problem
Binary Watch is a fascinating problem that combines binary representation with time display! ๐ฐ๏ธ
Imagine a digital watch that uses LEDs to show time in binary format. The watch has 4 LEDs for hours (representing 0-11) and 6 LEDs for minutes (representing 0-59). Each LED is either ON (1) or OFF (0).
Given an integer
Format Rules:
โข Hours: No leading zero (e.g., "1:00" not "01:00")
โข Minutes: Always two digits with leading zero if needed (e.g., "10:02" not "10:2")
Example: If 1 LED is on, possible times include "1:00", "2:00", "0:01", "0:02", etc.
Imagine a digital watch that uses LEDs to show time in binary format. The watch has 4 LEDs for hours (representing 0-11) and 6 LEDs for minutes (representing 0-59). Each LED is either ON (1) or OFF (0).
Given an integer
turnedOn representing the number of LEDs currently lit, your task is to find all possible times the watch could display.Format Rules:
โข Hours: No leading zero (e.g., "1:00" not "01:00")
โข Minutes: Always two digits with leading zero if needed (e.g., "10:02" not "10:2")
Example: If 1 LED is on, possible times include "1:00", "2:00", "0:01", "0:02", etc.
Input & Output
example_1.py โ Single LED
$
Input:
turnedOn = 1
โบ
Output:
["0:01","0:02","0:04","0:08","0:16","0:32","1:00","2:00","4:00","8:00"]
๐ก Note:
With 1 LED on, we can have either 1 hour bit (1,2,4,8) with 0 minute bits (0), or 0 hour bits (0) with 1 minute bit (1,2,4,8,16,32)
example_2.py โ Two LEDs
$
Input:
turnedOn = 9
โบ
Output:
[]
๐ก Note:
Maximum possible LEDs is 4 (hours) + 6 (minutes) = 10. But valid hours go up to 11 (binary 1011, 3 bits max) and valid minutes go up to 59 (binary 111011, 5 bits max). So maximum realistic is 3+5=8 bits.
example_3.py โ Zero LEDs
$
Input:
turnedOn = 0
โบ
Output:
["0:00"]
๐ก Note:
When no LEDs are on, both hours and minutes are 0, representing time "0:00"
Visualization
Tap to expand
Understanding the Visualization
1
LED Layout
4 LEDs for hours (8,4,2,1) and 6 LEDs for minutes (32,16,8,4,2,1)
2
Binary Representation
Each number is sum of powers of 2 where LEDs are on
3
Valid Combinations
Find all ways to distribute N LEDs between hours and minutes
4
Format Output
Convert to time format with proper zero padding
Key Takeaway
๐ฏ Key Insight: Pre-grouping hours and minutes by bit count transforms the problem from counting bits repeatedly to simple array lookups and combinations.
Time & Space Complexity
Time Complexity
O(1)
Still processes all valid combinations, but more efficiently
โ Linear Growth
Space Complexity
O(1)
Pre-computation arrays are small and constant size
โ Linear Space
Constraints
- 0 โค turnedOn โค 10
- Hours range from 0 to 11 (requires at most 4 LEDs)
- Minutes range from 0 to 59 (requires at most 6 LEDs)
- Total LEDs = 4 (hours) + 6 (minutes) = 10 maximum
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code