Medium
You are given an integer array nums.
You may perform the following operation any number of times, including zero times:
- Choose two integers
xandy, and replace every occurrence ofxinnumswithy.
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 = 1andy = 4to transformnumsinto[4, 2, 3, 3, 4, 4]. - Choose
x = 2andy = 4to transformnumsinto[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 = 1andy = 5to transformnumsinto[5, 2, 3, 4, 5]. - Choose
x = 4andy = 2to transformnumsinto[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 <= 1051 <= nums[i] <= 109