# 1122. Relative Sort Array

![](/files/-Mdoxz9-DPIsSHmwknzi)

there is a constrait : 0 <= arr1\[i], arr2\[i] <= 1000 .=> so we can use idea like couting sort&#x20;

time: O(m+n)

space: O(1), in-place

```java
class Solution {
    public int[] relativeSortArray(int[] arr1, int[] arr2) {
        int count[] = new int[1001];
        for (int n : arr1) { // m
            count[n]++;
        }
        int i = 0;
        for (int n : arr2) { // m
            while (count[n]-- > 0) {
                arr1[i++] = n;
            }
        }
        for (int n = 0; n < count.length; n++) { // n
            while (count[n]-- > 0) {
                arr1[i++] = n;
            }
        }
        return arr1;
    }
}
```

## use treemap

#### follow-up:&#x20;

#### **What if this constraint `0 <= arr1[i], arr2[i] <= 1000` doesn't exist?**

time: O(nLogn)

space: O(n)

```java
class Solution {
    public int[] relativeSortArray(int[] arr1, int[] arr2) {
        TreeMap<Integer, Integer> map = new TreeMap<>();
        for (int n : arr1) {
            map.put(n, map.getOrDefault(n, 0) + 1);
        }
        int i = 0;
        for (int n : arr2) {
            for (int j = 0; j < map.get(n); j++) {
                arr1[i++] = n;
            }
            map.remove(n);
        }
        for (int n : map.keySet()) {
            for (int j = 0; j < map.get(n); j++) {
                arr1[i++] = n;
            }
        }
        return arr1;
    }
}
```

## my origin idea:&#x20;

time: O(nLogn)

space: O(n)

```java
class Solution {
    public int[] relativeSortArray(int[] arr1, int[] arr2) {
        // brute force iterate arr2 , each arr2 element will scan arr1 once, so O(n*m)
        
        // put arr2 to hash table, build key with empty list
        // iterate arr1, put value into key arraylist, can't get the key, put into others arraylist
        // finally, iterate arr2 again, bring the correspond arraylist from hash table
        List<Integer> res = new ArrayList<>();

        Map<Integer, ArrayList<Integer>> map = new HashMap<>();
        for (int ar2 : arr2) {
            map.put(ar2, new ArrayList<>());
        }
        List<Integer> others = new ArrayList<>();
        for (int ar1 : arr1) {
            if (map.containsKey(ar1)) {
                List<Integer> list = map.get(ar1);
                list.add(ar1);
            } else {
                others.add(ar1);
            }
        }
        Collections.sort(others);
        for (int ar2 : arr2) {
            List<Integer> result = map.get(ar2);
            res.addAll(result);
        }
        res.addAll(others);
        return res.stream().mapToInt(i -> i).toArray();
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://timmybeeflin.gitbook.io/cracking-leetcode/couting-sort/1122.-relative-sort-array.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
