FastPrepMinimum Operations to Make Array Valid
Problems/Minimum Operations to Make Array Valid

Minimum Operations to Make Array Valid

Amazon logoAmazon● MediumOA

Updated Aug 2026

QuestionFunctionExamplesConstraints

Problem statement

You are given an array arr of n integers. In one operation, you choose any two distinct values x and y that appear in arr and replace every occurrence of x with y. Return the minimum number of operations needed to make the array valid.

An array is valid if every distinct value forms exactly one contiguous block. This means that for any value v, all occurrences of v must appear next to each other in a single continuous segment. Values do not all need to be the same. For example,

  • [1, 1, 2, 2, 3] is valid because each distinct value appears in one contiguous block.
  • [1, 2, 1, 3] is not valid because value 1 appears in two separated blocks, with value 2 between them.

If arr is already valid, return 0.

Function

minimumOperationsToMakeArrayValid(arr: int[]) → int

Examples

Example 1

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

Value 1 appears at two positions with value 2 between them. Replacing every occurrence of 2 with 1 yields [1, 1, 1], where 1 forms a single contiguous block. One operation is sufficient.

Example 2

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

One way to make the array valid is:

  • Replace every occurrence of 2 with 1, giving [1, 1, 3, 1, 1, 3].
  • Replace every occurrence of 3 with 1, giving [1, 1, 1, 1, 1, 1].

Now the only remaining distinct value is 1, so it forms a single contiguous block

Example 3

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

Constraints

  • 1 ≤ n ≤ 1000
  • 1 ≤ arr[i] ≤ 1000

More Amazon Online Assessment questions

  • Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
  • Minimum Grid InconvenienceOA · Seen Dec 2026
  • Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
  • Minimum Merge ConflictsOA · Seen Nov 2026
  • Lexicographically Largest IdentifierOA · Seen Nov 2026
  • Maximize Shipment Order by Sorting One WindowOA · Seen Nov 2026
  • Top K Frequent ElementsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Fill One Exact-Length Binary WindowOA · Seen Nov 2026
  • Maximize K-Element AND with an Increment BudgetOA · Seen Nov 2026
  • Get Smallest Base SegmentOA · Seen Nov 2026

Similar questions at other companies

  • Minimum Rounds to Complete All TasksInfosys · OAInfosys interview questions
  • Minimum Distinct Prefix CostChubb · OAChubb interview questions
  • Minimum Distinct Prefix CostGoldman Sachs · OAGoldman Sachs interview questions
  • Rearrange Songs by AuthorZomato / Eternal · OAZomato / Eternal interview questions
  • 🐘 Keep Them Apart 🦙QRT · OA
  • Maximum MEXAtlassian · OAAtlassian interview questions
  • Minimum Labels after Removing BoxesExpedia · OAExpedia interview questions
  • Minimize Distinct Item Types After RemovalsRoblox · OARoblox interview questions
  • Minimum Operations to Make a SubsequenceGoogle · OAGoogle interview questions
  • Maximum Distinct Values After SwapsAkuna Capital · OAAkuna Capital interview questions

Interview reports that mention this question

  • Amazon Full-Time OA Problems, Recent Reports (Part 4 of 5)

Amazon interview experiences

  • Amazon Experienced SDE Onsite Questions: 1 Experience
  • Amazon SDE Onsite Interview Questions: 9 Experiences
  • Amazon SDE II and L5 FDE Interview Questions: 4 Experiences
  • Amazon Full-Time OA Problems, Earlier Reports (Part 5 of 5)
  • Amazon New Grad OA Problems: Every Task We Have (Part 3 of 5)
See Amazon hiring insights
Saved
class Solution {
    public int minimumOperationsToMakeArrayValid(int[] arr) {
      // write your code here
    }
}
Checking account…