Description
Description
Editorial
Editorial
Solutions
Solutions
Submissions
Submissions
Medium

You are given an integer array nums.

You may perform the following operation any number of times, including zero times:

  • Choose two integers x and y, and replace every occurrence of x in nums with y.

Return the minimum number of operations required to transform nums into a .

 

Example 1:

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

Output: 2

Explanation:

One sequence of operations to transform nums into a palindromic array is:

  • Choose x = 1 and y = 4 to transform nums into [4, 2, 3, 3, 4, 4].
  • Choose x = 2 and y = 4 to transform nums into [4, 4, 3, 3, 4, 4], which is a palindromic array.

Example 2:

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

Output: 2

Explanation:

One sequence of operations to transform nums into a palindromic array is:

  • Choose x = 1 and y = 5 to transform nums into [5, 2, 3, 4, 5].
  • Choose x = 4 and y = 2 to transform nums into [5, 2, 3, 2, 5], which is a palindromic array.

Example 3:

Input: nums = [3,4,3]

Output: 0

Explanation:

nums is already a palindromic array.

 

Constraints:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 109
 
Code
Code
Testcase
Testcase
Test Result
Test Result