All problems
0224MediumMathBrainteaser

Greenhouse Vent Passes

Tracked in this browser only
Write code

Trains the technique from

LeetCode 319Bulb Switcher

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A greenhouse roof carries vent_count vents in one long row, numbered 1 through vent_count. Every vent starts shut.

The controller then runs vent_count sweeps over the row. On sweep k it visits each vent whose number is a multiple of k and flips that vent: a shut vent opens and an open vent shuts. Sweep 1 therefore touches every vent, sweep 2 touches vents 2, 4, 6 and so on, up to sweep vent_count, which touches only the last vent.

Return how many vents are open after the final sweep. If vent_count is 0 the roof has no vents and no sweeps are run.

Examples

Example 1

Input
vent_count = 8
Output
2

Sweep 1 opens all eight vents, sweep 2 flips the even-numbered ones, and each later sweep flips its own multiples. Two vents are open once sweep 8 has run.

Example 2

Input
vent_count = 27
Output
5

Five of the twenty-seven vents are open after the twenty-seventh sweep.

Example 3

Input
vent_count = 49
Output
7

Seven of the forty-nine vents are open once every sweep has run.

Constraints

  • 0 <= vent_count <= 10^9

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def count_open_vents(vent_count: int) -> int:
Java
public int countOpenVents(int ventCount)
September 7
Apply