Skip to main content

Maximum Equal Adjacent Pairs After at Most One Replacement - Solution & Explanation

Medium2 min read
Practice this problem

Problem Statement

You are given a 1-indexed integer array nums.

You can choose two distinct values x and y and perform the following operation at most once:

  • Replace every occurrence of x in nums with y.

Return the maximum possible number of pairs of adjacent elements that are equal after performing the operation.

 

Example 1:

Input: nums = [1,2,3,2]

Output: 2

Explanation:

  • One optimal solution is to choose x = 3 and y = 2.
  • The resulting array is [1, 2, 2, 2].
  • There are 2 pairs of adjacent elements that are equal: (nums[2], nums[3]) and (nums[3], nums[4]).
  • Therefore, the answer is 2.

Example 2:

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

Output: 4

Explanation:

  • One optimal solution is to choose x = 1 and y = 2.
  • The resulting array is [2, 2, 2, 2, 2].
  • There are 4 pairs of adjacent elements that are equal: (nums[1], nums[2]), (nums[2], nums[3]), (nums[3], nums[4]), and (nums[4], nums[5]).
  • Therefore, the answer is 4.

Example 3:

Input: nums = [1,1,1]

Output: 2

Explanation:

  • One optimal solution is to perform no operation.
  • Thus, the resulting array is [1, 1, 1].
  • There are 2 pairs of adjacent elements that are equal: (nums[1], nums[2]) and (nums[2], nums[3]).
  • Therefore, the answer is 2.

 

Constraints:

  • 2 <= nums.length <= 105
  • 1 <= nums[i] <= 109

Solutions for this problem are being prepared.

Try solving it yourself

Video Solution

Leetcode 4066 | Maximum Equal Adjacent Pairs After at Most One Replacement | weekly contest 521 • CodeWithMeGuys • 386 views views

Watch 3 more video solutions →

Ready to solve this problem?

Practice Maximum Equal Adjacent Pairs After at Most One Replacement with our built-in code editor and test cases.

Practice on FleetCode