# Animation illustration of a high-frequency interview question of an Internet factory - relative ranking of arrays

Posted by Trojan on Fri, 17 Sep 2021 22:22:52 +0200

## preface

Hello, I'm Xiaoxiong, a programmer from Huawei. Today, I'll bring you a high-frequency interview question related to arrays. This question is the interview question of big companies such as Google, Tencent, apple and Amazon, that is, the 1122 question on force buckle - the relative ranking of arrays.

This paper mainly introduces the strategy of counting sorting + hash table to solve this problem for your reference. I hope it will be helpful to you.

## Relative sorting of arrays

```I'll give you two arrays, arr1 and arr2，

arr2 The elements in are different

arr2 Each element in appears in the arr1 in

yes arr1 Sort the elements in so that arr1 Relative order and arr2 The relative order in is the same.

Not in arr2 The elements that appear in the need to be placed in ascending order arr1 End of.
```

Examples and tips

## Problem solving ideas

The topic has prompted 0 < = Arr1 [i], arr2 [i] < = 1000. Since this range is not large, it can be solved by sorting count sorting without comparison.

## Problem solving steps

1. Define an array hash with a length of 1001 to simulate the hash table.

2. Store all elements in arr1 in the hash array, where the key is the element in arr1, and the value is the number of occurrences of the element in arr1;

3. Define index i, initialized to 0, which is used to reset the element value in array arr1.

4. Traverse array arr2. As long as the element in arr2 exists in the array hash, assign it to arr1, and reduce the frequency of the element in the hash by one;

5. For elements that are not in array arr2, traverse the entire array hash. As long as the number of occurrences of the element is one or more, assign it to arr1, and reduce the frequency of the element in the hash by one.

## give an example

Take arr1 = [2,3,3,7,2,1], arr2 = [3,2,1] as an example, as shown in the following figure:

Example (before sorting)

After sorting as required, it is shown as follows:

arr1 sorted as required

Record the number of occurrences of elements in array arr1 with array hash, as shown in the following figure:

Traverse arr1 and record the occurrence times of its elements in the hash

Traverse array arr2, as shown in the following figure:

Traversal arr2

In the array hash, judge whether the number of occurrences of elements traversed into arr2 is greater than 0, as shown in the following figure:

Judge whether the number of occurrences of elements in arr2 in hash is greater than 0

Update the value of the element in array arr1, as shown in the following figure:

Update arr1

The complete sorting process is shown as follows:

Complete sorting process

### Show me the Code

C

```int* relativeSortArray(int* arr1, int arr1Size, int* arr2, int arr2Size, int* returnSize){
int index = 0;
int hash[1001] = {0};
for (int i = 0; i < arr1Size; ++i) {
hash[arr1[i]]++;
}

for (int i = 0; i < arr2Size; ++i) {
while (hash[arr2[i]] > 0) {
arr1[index++] = arr2[i];
hash[arr2[i]]--;
}
}

for (int i = 0; i < 1001; ++i) {
while (hash[i] > 0) {
arr1[index++] = i;
hash[i]--;
}
}

*returnSize = arr1Size;
return arr1;
}
```

C++

```vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) {
vector<int> hash(1001);
for (int num : arr1) {
hash[num]++;
}

int i = 0;
for (int num : arr2) {
while (hash[num] > 0) {
arr1[i++] = num;
hash[num]--;
}
}

for (int num = 0; num < hash.size(); num++) {
while (hash[num] > 0) {
arr1[i++] = num;
hash[num]--;
}
}

return arr1;
}
```

Java

```int[] relativeSortArray(int[] arr1, int[] arr2) {
int i = 0;
int [] hash = new int [1001];
for (int n : arr1) {
hash[n]++;
}

for (int n : arr2) {
while (hash[n] > 0) {
arr1[i++] = n;
hash[n]--;
}
}

for (int n = 0; n < hash.length; ++n) {
while (hash[n] > 0) {
arr1[i++] = n;
hash[n]--;
}
}

return arr1;
}
```

Python3

```def relativeSortArray(self, arr1: List[int], arr2: List[int]) -> List[int]:
arr = [0 for _ in range(1001)]
ans = []
for i in range(len(arr1)):
arr[arr1[i]] += 1
for i in range(len(arr2)):
while arr[arr2[i]] > 0:
ans.append(arr2[i])
arr[arr2[i]] -= 1
for i in range(len(arr)):
while arr[i] > 0:
ans.append(i)
arr[i] -= 1
return ans
```

### Complexity analysis

Time complexity: O(m + n), where m and N are the length of the array respectively. You need to traverse the two arrays once.

Space complexity: O(1), additional constant level storage space.

## Wonderful review of previous periods

Hash table wonderful solution of letter ectopic words

Greed allows you to split more balanced strings -- [force deduction daily question]

The animated illustration "two numbers add up" can be understood by primary school students

The linked list is orderly. How to merge quickly?