Find sorted order of Array after replacing digits with their given alternate

• Difficulty Level : Medium
• Last Updated : 03 Jun, 2022

Given an array A[] containing N integers and another array M[] containing alternate values of all digits from 0 to 9 (i.e; M[i] = j means, j is an alternate value of i( 0 â‰¤ i, j â‰¤ 9)), Perform following operations on the array:

• Replace all the digits of all elements of array A[] with their alternates given in M[].
• Sort the array according to the new value of the elements.
• Return the previous values in the sorted order of the new values (i.e. replace the new values with their previous values).

The task is to find this final sorted order of elements.

Note: The elements having the same new value should be sorted according to their position in the original array A[].

Examples:

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}

Approach: The idea to solve is as per following:

Firstly replace the digits with their alternates and then sort the newly formed value in increasing order. Then arrange the original array according to the sorted order of newly formed elements.

Follow the steps to solve the problem:

• Make a vector of pairs (say v).
• Iterate the array from i = 0 to N-1:
• Replace all digits of the elements with their alternates from M[] and find the new value (say temp).
• Make a pair having temp as the first element and A[i] as second element.
• Push the pair in v.
• Sort the vector v in increasing order of the first values of the pairs (i.e. the newly formed values after replacing digits with alternates).
• Now arrange the array such that A[i] is the same as the second value of v[i].

Below is the implementation for the above approach:

C++

 `// C++ Code for above approach`   `#include ` `using` `namespace` `std;`   `// Function to find new value of an` `// integer by mapping digits from M` `int` `newValue(``int` `num, ``int` `M[])` `{` `    ``string s = to_string(num);` `    ``int` `New_Value = 0, x = 0;` `    ``for` `(``int` `i = s.size() - 1; i >= 0; i--) {` `        ``New_Value += (M[s[i] - 0]` `                      ``* ``pow``(10, x));` `        ``x++;` `    ``}` `    ``return` `New_Value;` `}`   `// Comparator function for sorting` `bool` `comp(pair<``int``, ``int``>& p1,` `          ``pair<``int``, ``int``>& p2)` `{` `    ``return` `p1.first < p2.first;` `}`   `// Function to Sort the given array` `// according to the New Value of digits` `void` `sortArray(``int` `N, ``int` `A[], ``int` `M[])` `{` `    ``// Vector to store pair of elements` `    ``// and their new values` `    ``vector > v;`   `    ``// Pushing pairs of New value of elements` `    ``// and elements into vector v` `    ``for` `(``int` `i = 0; i < N; i++) {` `        ``int` `New_Value = newValue(A[i], M);` `        ``v.push_back({ New_Value, A[i] });` `    ``}`   `    ``// Sorting the vector "v" in` `    ``// increasing order of first` `    ``// element of pair` `    ``sort(v.begin(), v.end(), comp);`   `    ``// Storing the values of second value` `    ``// of vector "v" in array "A"` `    ``for` `(``int` `i = 0; i < N; i++) {` `        ``A[i] = v[i].second;` `    ``}` `    ``for` `(``int` `i = 0; i < N; i++) {` `        ``cout << A[i] << ``" "``;` `    ``}` `}`   `// Driver Code` `int` `main()` `{` `    ``int` `N = 3;` `    ``int` `A[] = { 991, 338, 38 };` `    ``int` `M[] = { 8, 9, 4, 0, 2, 1, 3, 5, 7, 6 };`   `    ``// Function call` `    ``sortArray(N, A, M);` `    ``return` `0;` `}`

Java

 `import` `java.util.*;` `import` `java.io.*;`   `// Java program for the above approach` `class` `GFG{`   `  ``// Function to find new value of an` `  ``// integer by mapping digits from M` `  ``public` `static` `int` `newValue(``int` `num, ``int` `M[])` `  ``{` `    ``String s = Integer.toString(num);` `    ``int` `New_Value = ``0``;` `    ``int` `x = ``0``;` `    ``for` `(``int` `i = s.length() - ``1``; i >= ``0``; i--) {` `      ``New_Value += (M[s.charAt(i) - ``'0'``] * Math.pow(``10``, x));` `      ``x++;` `    ``}` `    ``return` `New_Value;` `  ``}`   `  ``// Function to Sort the given array` `  ``// according to the New Value of digits` `  ``public` `static` `void` `sortArray(``int` `N, ``int` `A[], ``int` `M[])` `  ``{` `    ``// Vector to store pair of elements` `    ``// and their new values` `    ``ArrayList v = ``new` `ArrayList();`   `    ``// Pushing pairs of New value of elements` `    ``// and elements into vector v` `    ``for` `(``int` `i = ``0``; i < N; i++) {` `      ``int` `New_Value = newValue(A[i], M);` `      ``v.add(``new` `pair(New_Value, A[i]));` `    ``}`   `    ``// Sorting the vector "v" in` `    ``// increasing order of first` `    ``// element of pair` `    ``Collections.sort(v, ``new` `comp());`   `    ``// Storing the values of second value` `    ``// of vector "v" in array "A"` `    ``for` `(``int` `i = ``0``; i < N; i++) {` `      ``A[i] = v.get(i).y;` `    ``}` `    ``for` `(``int` `i = ``0``; i < N; i++) {` `      ``System.out.print(A[i] + ``" "``);` `    ``}` `  ``}`   `  ``// Driver code` `  ``public` `static` `void` `main(String args[])` `  ``{` `    ``int` `N = ``3``;` `    ``int` `A[] = {``991``, ``338``, ``38``};` `    ``int` `M[] = {``8``, ``9``, ``4``, ``0``, ``2``, ``1``, ``3``, ``5``, ``7``, ``6``};`   `    ``// Function call` `    ``sortArray(N, A, M);` `  ``}` `}`   `// Custom pair class` `class` `pair{` `  ``Integer x;` `  ``Integer y;` `  ``pair(``int` `x,``int` `y){` `    ``this``.x = x;` `    ``this``.y = y;` `  ``}` `}`   `// Comparator for pair class` `class` `comp ``implements` `Comparator{` `  ``public` `int` `compare(pair o1, pair o2){` `    ``return` `o1.x.compareTo(o2.x);` `  ``}` `}`   `// This code is contributed by subhamgoyal2014.`

Python3

 `# Python code for the above approach`   `# Function to find new value of an` `# integer by mapping digits from M` `from` `functools ``import` `cmp_to_key` `import` `math`   `def` `mycmp(a,b):` `    ``return` `a[``0``] ``-` `b[``0``]`   `def` `newValue(num, M):` `    ``s ``=` `str``(num)` `    ``New_Value,x ``=` `0``,``0` `    ``for` `i ``in` `range``(``len``(s) ``-` `1``,``-``1``,``-``1``):` `        ``New_Value ``+``=` `(M[``ord``(s[i]) ``-` `ord``(``'0'``)]``*` `math.``pow``(``10``, x))` `        ``x ``+``=` `1` `    ``return` `New_Value`   `# Function to Sort the given array` `# according to the New Value of digits` `def` `sortArray(N, A, M): `   `    ``# Vector to store pair of elements` `    ``# and their new values` `    ``v ``=` `[]`   `    ``# Pushing pairs of New value of elements` `    ``# and elements into vector v` `    ``for` `i ``in` `range``(N):` `        ``New_Value ``=` `newValue(A[i], M)` `        ``v.append([New_Value,A[i]])`   `    ``# Sorting the vector "v" in` `    ``# increasing order of first` `    ``# element of pair` `    ``v.sort(key ``=` `cmp_to_key(mycmp))`   `    ``# Storing the values of second value` `    ``# of vector "v" in array "A"` `    ``for` `i ``in` `range``(N):` `        ``A[i] ``=` `v[i][``1``]`   `    ``for` `i ``in` `range``(N):` `        ``print``(A[i],end ``=` `" "``)`   `# Driver Code` `N ``=` `3` `A ``=` `[``991``, ``338``, ``38``]` `M ``=` `[``8``, ``9``, ``4``, ``0``, ``2``, ``1``, ``3``, ``5``, ``7``, ``6``]`   `# Function call` `sortArray(N, A, M)`   `# This code is contributed by shinjanpatra`

Javascript

 ``

Output

`338 38 991 `

Time Complexity: O(N*log(N))
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up
Related Articles