Grokking Advanced Coding Patterns for Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Relative Sort Array (easy)
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem Statement

Given two arrays arr1 of length n and arr2 of length m, sort the elements of arr1 such that the relative ordering of items in arr1 is the same as in arr2. Elements that do not appear in arr2 should be placed at the end of arr1 in ascending order.

It is given that elements of arr2 are distinct, and all elements in arr2 are also in arr1.

Examples

Example 1:

  • Input: arr1 = [3, 5, 2, 1, 6, 4, 5, 6], arr2 = [5, 6, 4, 1]
  • Expected Output: [5, 5, 6, 6, 4, 1, 2, 3]
  • Justification: Elements 5, 6, 4 and 1 from arr2 are placed in arr1 first in the same order as in arr2. The remaining elements 2 and 3 are placed at the end in ascending order.

Example 2:

  • Input: arr1 = [8, 3, 9, 1, 7, 5], arr2 = [3, 9, 8]
  • Expected Output: [3, 9, 8, 1, 5, 7]
  • Justification: Elements 3, 9 and 8 from arr2 are placed in arr1 first in the same order as in arr2. Remaining elements 1, 5 and 7 are placed at the end in ascending order.

Example 3:

  • Input: arr1 = [10, 10, 7, 10, 7, 9], arr2 = [10, 7]
  • Expected Output: [10, 10, 10, 7, 7, 9]
  • Justification: Elements 10 and 7 from arr2 are placed in arr1 first in the same order as in arr2. The remaining element 9 is placed at the end in ascending order.

Constraints:

  • 1 <= arr1.length, arr2.length <= 1000
  • 0 <= arr1[i], arr2[i] <= 1000
  • All the elements of arr2 are distinct.
  • Each arr2[i] is in arr1.

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible