# Print elements of an array according to the order defined by another array | set 2

Given two arrays a1[] and a2[], print elements of a1 in such a way that the relative order among the elements will be the same as those are in a2. That is, elements which come before in the array a2[], print those elements first from the array a1[]. For the elements not present in a2, print them at last in sorted order.

It is also given that the number of elements in a2[] is smaller than or equal to the number of elements in a1[], and a2[] has all distinct elements.

**Example:**

Input:a1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8}

a2[] = {2, 1, 8, 3}

Output:2 2 1 1 8 8 3 5 6 7 9

Input:a1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8}

a2[] = {1, 10, 11}

Output:1 1 2 2 3 5 6 7 8 8 9

**Simple Approach:** We create a temporary array and a visited array where the temporary array is used to copy contents of a1[] to it and visited array is used to mark those elements in the temporary array which are copied to a1[]. Then sorting the temporary array and doing a binary search for every element of a2[] in a1[]. You can find the solution here.

**Efficient Approach:** We can print the elements of a1[] according to the order defined by a2[] using **map in c++** in O(mlog(n)) time. We traverse through a1[] and store the frequency of every number in a map. Then we traverse through a2[] and check if the number is present in the map. If the number is present, then print it that many times and erase the number from the map. Print the rest of the numbers present in the map sequentially as numbers are stored in the map in sorted order.

Below is the implementation of the above approach:

## C++

`// A C++ program to print an array according ` `// to the order defined by another array ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to print an array according ` `// to the order defined by another array ` `void` `print_in_order(` `int` `a1[], ` `int` `a2[], ` `int` `n, ` `int` `m) ` `{ ` ` ` `// Declaring map and iterator ` ` ` `map<` `int` `, ` `int` `> mp; ` ` ` `map<` `int` `, ` `int` `>::iterator itr; ` ` ` ` ` `// Store the frequncy of each ` ` ` `// number of a1[] int the map ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `mp[a1[i]]++; ` ` ` ` ` `// Traverse through a2[] ` ` ` `for` `(` `int` `i = 0; i < m; i++) { ` ` ` `// Check whether number ` ` ` `// is present in map or not ` ` ` ` ` `if` `(mp.find(a2[i]) != mp.end()) { ` ` ` `itr = mp.find(a2[i]); ` ` ` ` ` `// Print that number that ` ` ` `// many times of its frequncy ` ` ` `for` `(` `int` `j = 0; j < itr->second; j++) ` ` ` `cout << itr->first << ` `" "` `; ` ` ` `mp.erase(a2[i]); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Print those numbers that are not ` ` ` `// present in a2[] ` ` ` `for` `(itr = mp.begin(); itr != mp.end(); itr++) { ` ` ` `for` `(` `int` `j = 0; j < itr->second; j++) ` ` ` `cout << itr->first << ` `" "` `; ` ` ` `} ` ` ` ` ` `cout << endl; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a1[] = { 2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8 }; ` ` ` `int` `a2[] = { 2, 1, 8, 3 }; ` ` ` `int` `n = ` `sizeof` `(a1) / ` `sizeof` `(a1[0]); ` ` ` `int` `m = ` `sizeof` `(a2) / ` `sizeof` `(a2[0]); ` ` ` ` ` `print_in_order(a1, a2, n, m); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# A Python3 program to print an array according ` `# to the order defined by another array ` ` ` `# Function to print an array according ` `# to the order defined by another array ` `def` `print_in_order(a1, a2, n, m) : ` ` ` ` ` `# Declaring map and iterator ` ` ` `mp ` `=` `dict` `.fromkeys(a1,` `0` `); ` ` ` ` ` `# Store the frequncy of each ` ` ` `# number of a1[] int the map ` ` ` `for` `i ` `in` `range` `(n) : ` ` ` `mp[a1[i]] ` `+` `=` `1` `; ` ` ` ` ` `# Traverse through a2[] ` ` ` `for` `i ` `in` `range` `(m) : ` ` ` `# Check whether number ` ` ` `# is present in map or not ` ` ` ` ` `if` `a2[i] ` `in` `mp.keys() : ` ` ` ` ` `# Print that number that ` ` ` `# many times of its frequncy ` ` ` `for` `j ` `in` `range` `(mp[a2[i]]) : ` ` ` `print` `(a2[i],end` `=` `" "` `); ` ` ` ` ` `del` `(mp[a2[i]]); ` ` ` ` ` `# Print those numbers that are not ` ` ` `# present in a2[] ` ` ` `for` `key,value ` `in` `mp.items() : ` ` ` `for` `j ` `in` `range` `(value) : ` ` ` `print` `(key,end` `=` `" "` `); ` ` ` ` ` `print` `(); ` ` ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `a1 ` `=` `[ ` `2` `, ` `1` `, ` `2` `, ` `5` `, ` `7` `, ` `1` `, ` `9` `, ` `3` `, ` `6` `, ` `8` `, ` `8` `]; ` ` ` `a2 ` `=` `[ ` `2` `, ` `1` `, ` `8` `, ` `3` `]; ` ` ` `n ` `=` `len` `(a1); ` ` ` `m ` `=` `len` `(a2); ` ` ` ` ` `print_in_order(a1, a2, n, m); ` ` ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

2 2 1 1 8 8 3 5 6 7 9

## Recommended Posts:

- Sort an array according to the order defined by another array
- Print the last occurrence of elements in array in relative order
- Print n smallest elements from given array in their original order
- Print array elements in alternatively increasing and decreasing order
- Sort the array of strings according to alphabetical order defined by another string
- Print array of strings in sorted order without copying one string into another
- Rearrange all elements of array which are multiples of x in increasing order
- Rearrange all elements of array which are multiples of x in decreasing order
- Find k maximum elements of array in original order
- Sort the linked list in the order of elements appearing in the array
- Shortest path to traverse all the elements of a circular array in increasing order
- Program to print Sum of even and odd elements in an array
- Print array elements that are divisible by at-least one other
- Print sorted distinct elements of array
- Print All Distinct Elements of a given integer array

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.