FastPrepMake Value Groups Contiguous
Problems/Make Value Groups Contiguous

Make Value Groups Contiguous

Amazon logoAmazon● MediumNEW GRADOA
QuestionFunctionExamplesConstraints

Problem statement

You are given an integer array arr. In one operation, choose two values x and y (where y may be any value, including an existing value in the array), and replace every occurrence of x in the array with y.

An array is called contiguous by value if, for every distinct value present, all occurrences of that value appear in one uninterrupted block with no other values in between.

Return the minimum number of operations needed to make the array contiguous by value.

Function

minOperationsToMakeValuesContiguous(arr: int[]) → int

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

Examples

Example 1

arr = [1, 2, 1]return = 1

Replace every 2 with 1 to get [1, 1, 1]. Now the only distinct value, 1, appears in one contiguous block. This requires 1 operation.

Example 2

arr = [1, 2, 3]return = 0

Every value already occupies a single contiguous block: 1 appears once, 2 appears once, 3 appears once. No operations are needed.

Constraints

  • 1 <= arr.length <= 105
  • 1 <= arr[i] <= 105
  • Each operation replaces all occurrences of one chosen value x globally with any chosen value y.
  • After all operations are complete, each distinct value remaining in the array must appear in a single contiguous block.
  • The minimum number of operations equals the total number of distinct values minus the maximum number of distinct values that can simultaneously remain unmerged (i.e., each already occupies a single contiguous block in the original array independently of the others).

More Amazon problems

  • Count Adjacent Symbol Pairs for BPEONSITE INTERVIEW · Seen Nov 2026
  • First Non-Repeating CharacterOA · ONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
See Amazon hiring insights
Saved
class Solution {
    public int minOperationsToMakeValuesContiguous(int[] arr) {
        // write your code here
    }

}
Checking account…