Maximum Equal Neighbor Pairs After One Value Replacement
Quick Overview
Maximize equal adjacent pairs after at most one global replacement of every occurrence of a value, counting overlapping neighboring pairs and preserving existing equalities.
Maximum Equal Neighbor Pairs After One Value Replacement
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Other
# Maximum Equal Neighbor Pairs After One Value Replacement
You are given an integer array `nums`, with positions numbered from `1` through its length.
You may perform this operation at most once: choose two distinct values `x` and `y`, then replace every occurrence of `x` in the array with `y`.
Return the maximum possible number of equal adjacent pairs. These are consecutive-position pairs `(1, 2)`, `(2, 3)`, and so on whose values are equal after the operation.
### Function
`int maxEqualAdjacentPairs(vector<int>& nums)`
### Example 1
**Input:** `nums = [4, 4, 7, 4, 7, 9, 9]`
**Output:** `5`
**Explanation:** Replace every `7` with `4`, giving `[4, 4, 4, 4, 4, 9, 9]`. The first run contributes four equal adjacent pairs, and the final two `9`s contribute one, totaling `5`.
### Example 2
**Input:** `nums = [6, 6, 6, 6]`
**Output:** `3`
**Explanation:** Make no replacement. All three adjacent pairs are already equal.
### Constraints
- `2 <= nums.length <= 100000`
- `1 <= nums[i] <= 1000000000`
### Clarifications
- The operation replaces all occurrences of one chosen value, not one array element.
- Adjacent pairs may overlap. For example, three consecutive equal values contribute two pairs.
- Only one global choice of distinct `x` and `y` is allowed, or the operation may be skipped.
- Return the maximum count, not the resulting array or the chosen values.
### What a Strong Answer Covers
- Accounting for a value replacement's effects across the entire array.
- Counting both orientations of a neighboring value pair and preserving already-equal pairs.
- Handling repeated runs and large values without inspecting every possible numeric value.
Overview: Maximize equal adjacent pairs after at most one global replacement of every occurrence of a value, counting overlapping neighboring pairs and preserving existing equalities.
Maximum Equal Neighbor Pairs After One Value Replacement
Amazon
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0
Maximum Equal Neighbor Pairs After One Value Replacement
You are given an integer array nums, with positions numbered from 1 through its length.
You may perform this operation at most once: choose two distinct values x and y, then replace every occurrence of x in the array with y.
Return the maximum possible number of equal adjacent pairs. These are consecutive-position pairs (1, 2), (2, 3), and so on whose values are equal after the operation.
Function
int maxEqualAdjacentPairs(vector<int>& nums)
Example 1
Input:nums = [4, 4, 7, 4, 7, 9, 9]
Output:5
Explanation: Replace every 7 with 4, giving [4, 4, 4, 4, 4, 9, 9]. The first run contributes four equal adjacent pairs, and the final two 9s contribute one, totaling 5.
Example 2
Input:nums = [6, 6, 6, 6]
Output:3
Explanation: Make no replacement. All three adjacent pairs are already equal.
Constraints
2 <= nums.length <= 100000
1 <= nums[i] <= 1000000000
Clarifications
The operation replaces all occurrences of one chosen value, not one array element.
Adjacent pairs may overlap. For example, three consecutive equal values contribute two pairs.
Only one global choice of distinct
x
and
y
is allowed, or the operation may be skipped.
Return the maximum count, not the resulting array or the chosen values.
What a Strong Answer Covers Guidance
Accounting for a value replacement's effects across the entire array.
Counting both orientations of a neighboring value pair and preserving already-equal pairs.
Handling repeated runs and large values without inspecting every possible numeric value.