Disturbed People

There is a house with n flats arranged in a straight line along the main street.

You are given a binary array nums of length n, where:

  • nums[i] = 1 → the light in the i-th flat is ON
  • nums[i] = 0 → the light in the i-th flat is OFF

A flat with light OFF is disturbed if both its immediate neighbors have lights ON.

Return the minimum number k such that if you turn OFF the lights in exactly k distinct flats (i.e., change 1 → 0), then no flat remains disturbed.

Each flat can be chosen at most once.

Example 1:

Input: nums = [1, 0, 1]

Output: 1

Explanation:

Flat at index 1 is disturbed because both neighbors have light ON.

Turning OFF either index 0 or index 2 removes the disturbance.

So, minimum k = 1.

Example 2:

Input: nums = [1, 0, 1, 0, 1]

Output: 1

Explanation:

  • Index 1 is disturbed (1 0 1)
  • Index 3 is disturbed (1 0 1)

We must break both patterns.

Turning OFF index 2 fixes the first & 2nd disturbance.

Now Your Turn!

Pick the correct output for the given input

Input: nums = [0, 1, 0, 1, 0]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 3 ≤ nums.length ≤ 100
  • nums[i] ∈ {0, 1}
  • You may only turn OFF lights (1 → 0)
  • Each flat can be modified at most once

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
int disturbedPeople(vector<int> &nums) {
// Your code goes here
}
};
Test Case

Input:

Nums