Back to problems

Merge Values into Blocks

Algorithm · Amazon · Hard

You have an integer array arr containing n elements. During one operation, select two different values x and y that occur in arr, then change all instances of x into y. Find the smallest number of operations that makes the array valid. An array is valid when each distinct value occupies one contiguous block. In other words, all appearances of any value v must lie together in one uninterrupted segment. The array may contain multiple blocks belonging to different values. For…

Checking your access…