Skip to main content

Maximum Equal Adjacent Pairs After at Most One Replacement - Video Solutions

Medium

Leetcode 4066 | Maximum Equal Adjacent Pairs After at Most One Replacement | weekly contest 521

CodeWithMeGuys
9:01386 views
4 video solutions available

Maximum Equal Adjacent Pairs After at Most One Replacement - Video Solution

Watch 4 video solutions for Maximum Equal Adjacent Pairs After at Most One Replacement, a medium level problem. This walkthrough by CodeWithMeGuys has 386 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

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
Read full problem with examples