# 401. Binary Watch(Solution || Leetcode easy || Java)

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 be consist of two digits and may contain a leading zero.

• For example, "10:2" is not valid. It should be "10:02".

Example 1:

Input: turnedOn = 1
Output: ["0:01","0:02","0:04","0:08","0:16","0:32","1:00","2:00","4:00","8:00"]

Example 2:

Input: turnedOn = 9
Output: []

Constraints:

• 0 <= turnedOn <= 10

# SOLUTION:

class Solution {

//Create a list of string to store answers
List<String> ans = new ArrayList<>();

//Start running loop to check for the hours
for(int hour = 0; hour < 12; hour++){

//Start running loop to check the number of bits in minutes
for(int min = 0; min < 60; min++){

//If the number of bits of hour and minute is equal to turnedOn then
//add that time in our list
if(Integer.bitCount(hour) + Integer.bitCount(min) == turnedOn){
String newTime = hour + ":";

//If min is a single digit number, let say is 4 then, add a 0 before it
if(min < 10){
newTime += 0;
}

newTime += min;

//add that time in our ans.
}

}
}

return ans;
}
}

Runtime10 ms

Beats

77.83%

Memory41.1 MB

Beats

93.10%

Thank you for reading. If you have any queries then, please let me know in the comment section. I will surely be responsive toward it.

--

--

Solutions to all your coding related problems at one point. DSA question on daily basis and much more.