Tap here to switch tabs
Problem
Submissions

Problem: Relative Sort Array

med
30 min
Try to solve the Relative Sort Array problem.

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:

  • 1≤1 \leq arr1.length, arr2.length ≤1000\leq 1000

  • 0≤0 \leq arr1[i], arr2[i] ≤1000\leq 1000

  • All elements of arr2 are distinct

  • Each arr2[i] is in arr1

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Relative Sort Array

med
30 min
Try to solve the Relative Sort Array problem.

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:

  • 1≤1 \leq arr1.length, arr2.length ≤1000\leq 1000

  • 0≤0 \leq arr1[i], arr2[i] ≤1000\leq 1000

  • All elements of arr2 are distinct

  • Each arr2[i] is in arr1