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 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
Binary Watch LED LayoutBinary Watch FaceHOURS (0-11)8421MINUTES (0-59)321684214:10Hours: 4 (binary 100) = 1 LED | Minutes: 10 (binary 1010) = 2 LEDsTotal LEDs ON: 3Problem: Given N LEDs are ON, find all possible timesSolution: Try all combinations of hour_bits + minute_bits = N
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

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Pre-computation arrays are small and constant size

n
2n
โœ“ 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
Asked in
Google 15 Amazon 12 Apple 8 Microsoft 6
23.8K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen