The task is to find this final sorted order of elements.
Input: N = 3, A = {991, 338, 38}, M = {8, 9, 4, 0, 2, 1, 3, 5, 7, 6}
Output: {338, 38, 991}
Explanation: Calculation of New Value of 991: M[9] = 6, M[9] = 6, M[1] = 9. So 991 => 669
New value of 338 is 007 i.e. equal to 7.
New value of 38 is 07 or 7.
So, the sorted array of new values is {7, 7, 669}.
So the sorted order is {338, 38, 991}.
Because both 338 and 38 have same value 7 and 338 comes before 38 in actual array.
Input: N = 5, A[] = {20, 25, 10, 5, 77}, M = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
Output: {5, 10, 20, 25, 77}