Problem Brief
Make Value Groups Contiguous
NEW GRADOA
See Amazon online assessment and hiring insightsYou are given an integer array arr. In one operation, choose two values x and y, and replace every occurrence of x in the array with y.
An array is called contiguous by value if, for every distinct value, all occurrences of that value appear in one uninterrupted block.
Return the minimum number of operations needed to make the array contiguous by value.
Function Description
Complete the function minOperationsToMakeValuesContiguous in the editor below.
minOperationsToMakeValuesContiguous has the following parameter:
int[] arr: the input array
Returns
int: the minimum number of replacement operations.
1Example 1
Input
arr = [1, 2, 1]
Output
1
Explanation
Replace every 2 with 1 to get [1, 1, 1].
2Example 2
Input
arr = [1, 2, 3]
Output
0
Explanation
Every value already occupies a single contiguous block.
Constraints
Limits and guarantees your solution can rely on.
- Each operation replaces all occurrences of one chosen value globally.
- After the final array is formed, each distinct value must appear in a single block.