Back to DSA sheet

Can Place Flowers

EasyArrays

A flowerbed is a row of plots, each 0 (empty) or 1 (planted). Flowers cannot be planted in adjacent plots. Decide whether you can plant exactly n more flowers without breaking that rule.

Input format (stdin): the first line has m (bed length) and n (flowers to plant). The second line has m values (0 or 1). Output true or false.

Examples
Input: 5 1 1 0 0 0 1
Output: true
The middle empty plot can hold one flower.
Input: 5 2 1 0 0 0 1
Output: false
Only one flower fits without touching another.
Constraints
  • 1 <= m <= 2*10^4
  • 0 <= n <= m
  • Each plot is 0 or 1
Sheets
NeetCode 250
can-place-flowers.cpp3 sample tests
Loading editor
Test results

Run the sample tests to check your solution against expected output.

Custom input (stdin)
Output

Run your code to see its output.