Search⌘ K
AI Features

Solution: Relative Sort Array

C# solution for the Relative Sort Array problem using the Sort and Search pattern.

Statement

Given two integer arrays arr1 and arr2, reorder the elements of arr1 so that:

All values that also appear in arr2 come first, and they appear in the same relative order as in arr2. For each value v in arr2, all occurrences of v in arr1 must be placed together.

After placing all elements that are in arr2, append the remaining elements of arr1 that do not appear in arr2, sorted in ascending order.

Return the reordered array.

Note: arr2 contains distinct values, and every value in arr2 appears in arr1.

Constraints:

  • 11 \leq arr1.length, arr2.length 1000\leq 1000

  • 00 \leq ...