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++ Code for above approach #include <bits/stdc++.h> 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<pair< int , int > > 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;
} |
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<pair> v = new ArrayList<pair>();
// 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<pair>{
public int compare(pair o1, pair o2){
return o1.x.compareTo(o2.x);
}
} // This code is contributed by subhamgoyal2014. |
# 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 |
// C# Code for above approach using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
// Comparator for sorting a 2d list. class GFG2 : IComparer<List< int >>
{ public int Compare(List< int > x, List< int > y)
{
// "CompareTo()" method
return x.ElementAt(0).CompareTo(y.ElementAt(0));
}
} class GFG1 {
// Function to find new value of an
// integer by mapping digits from M
public static int newValue( int num, int [] M)
{
String s = num.ToString();
int New_Value = 0;
int x = 0;
for ( int i = s.Length - 1; i >= 0; i--) {
int indx = Convert.ToInt32( new string (s[i], 1));
New_Value += (M[indx] * ( int )Math.Pow(10, x));
x = x + 1;
}
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
List<List< int >> v = new List<List< int >>();
// 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 List< int > { New_Value, A[i]});
}
// Sorting the vector "v" in
// increasing order of first
// element of pair
GFG2 gg = new GFG2();
v.Sort(gg);
// Storing the values of second value
// of vector "v" in array "A"
int indx = 0;
foreach ( var ele in v){
foreach ( var n_ele in ele){
A[indx] = n_ele;
}
indx = indx + 1;
}
for ( int i = 0; i < N; i++) {
Console.Write(A[i] + " " );
}
Console.WriteLine();
}
static void 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);
}
} // The code is contributed by Nidhi goel. |
<script> // JavaScript code for the above approach
// Function to find new value of an
// integer by mapping digits from M
function newValue(num, M) {
let s = (num).toString();
let New_Value = 0, x = 0;
for (let i = s.length - 1; i >= 0; i--) {
New_Value += (M[s[i] - 0]
* Math.pow(10, x));
x++;
}
return New_Value;
}
// Function to Sort the given array
// according to the New Value of digits
function sortArray(N, A, M)
{
// Vector to store pair of elements
// and their new values
let v = [];
// Pushing pairs of New value of elements
// and elements into vector v
for (let i = 0; i < N; i++) {
let New_Value = newValue(A[i], M);
v.push({ first: New_Value, second: A[i] });
}
// Sorting the vector "v" in
// increasing order of first
// element of pair
v.sort( function (a, b) { return a.first - b.first })
// Storing the values of second value
// of vector "v" in array "A"
for (let i = 0; i < N; i++) {
A[i] = v[i].second;
}
for (let i = 0; i < N; i++) {
document.write(A[i] + " " );
}
}
// Driver Code
let N = 3;
let A = [991, 338, 38];
let M = [8, 9, 4, 0, 2, 1, 3, 5, 7, 6];
// Function call
sortArray(N, A, M);
// This code is contributed by Potta Lokesh
</script> |
338 38 991
Time Complexity: O(N*log(N))
Auxiliary Space: O(N)