Initially n bulbs are off. In round i, toggle every ith bulb. After n rounds, count bulbs that are on.

Input: n=3 → Output: 1Input: n=0 → Output: 0

A bulb i is toggled for each divisor of i. It ends on only if toggled an odd number of times, which means i is a perfect square.

class Solution { public int bulbSwitch(int n) { return (int) Math.sqrt(n); } }