A binary watch has 4 LEDs on the top to represent the hours (0-11), and 6 LEDs on the bottom to representΒ the minutes (0-59). Each LED represents a zero or one, with the least significant bit on the right.
For example, the below binary watch reads "4:51".
Given an integer turnedOn which represents the number of LEDs that are currently on (ignoring the PM), return all possible times the watch could represent. You may return the answer in any order.
The hour must not contain a leading zero.
For example, "01:00" is not valid. It should be "1:00".
The minute mustΒ consist of two digits and may contain a leading zero.
For example, "10:2" is not valid. It should be "10:02".
The problem can be converted to finding all possible combinations of \(i \in [0, 12)\) and \(j \in [0, 60)\).
A valid combination must satisfy the condition that the number of 1s in the binary representation of \(i\) plus the number of 1s in the binary representation of \(j\) equals \(\textit{turnedOn}\).
The time complexity is \(O(1)\), and the space complexity is \(O(1)\).
We can use \(10\) binary bits to represent the watch, where the first \(4\) bits represent hours and the last \(6\) bits represent minutes. Enumerate each number in \([0, 2^{10})\), check if the number of 1s in its binary representation equals \(\textit{turnedOn}\), and if so, convert it to time format and add it to the answer.
The time complexity is \(O(1)\), and the space complexity is \(O(1)\).