Show problems as
Classic problem #2170MediumArrayHash TableGreedyCounting

Minimum Operations to Make the Array Alternating

Real-world version: Fewest Changes to Make the Strip Alternate
Tracked in this browser only
Read Minimum Operations to Make the Array Alternating on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A strip of positive readings reads readings. The strip alternates when every two readings that sit two places apart are equal and no two neighbouring readings are equal. A strip of one reading alternates already.

One change replaces a single reading with any positive whole number. Return the fewest changes that leave the strip alternating.

Examples

Example 1

Input
readings = [2, 1, 2, 1]
Output
0

The strip already alternates: the even positions all read two, the odd ones all read one, and the two differ.

Example 2

Input
readings = [1, 1, 1, 1]
Output
2

Both sides want to read one, but they must differ, so one side has to change every one of its two readings.

Example 3

Input
readings = [5, 5]
Output
1

The two readings are equal and sit next to each other, so one of them has to change.

Constraints

  • 1 <= readings.length <= 10^5
  • 1 <= readings[i] <= 10^5

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def minimum_operations(readings: list[int]) -> int:
Java
public int minimumOperations(int[] readings)