Find sorted order of Array after replacing digits with their given alternate
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++
#include <bits/stdc++.h>
using namespace std;
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;
}
bool comp(pair< int , int >& p1,
pair< int , int >& p2)
{
return p1.first < p2.first;
}
void sortArray( int N, int A[], int M[])
{
vector<pair< int , int > > v;
for ( int i = 0; i < N; i++) {
int New_Value = newValue(A[i], M);
v.push_back({ New_Value, A[i] });
}
sort(v.begin(), v.end(), comp);
for ( int i = 0; i < N; i++) {
A[i] = v[i].second;
}
for ( int i = 0; i < N; i++) {
cout << A[i] << " " ;
}
}
int main()
{
int N = 3;
int A[] = { 991, 338, 38 };
int M[] = { 8, 9, 4, 0, 2, 1, 3, 5, 7, 6 };
sortArray(N, A, M);
return 0;
}
|
Java
import java.util.*;
import java.io.*;
class GFG{
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;
}
public static void sortArray( int N, int A[], int M[])
{
ArrayList<pair> v = new ArrayList<pair>();
for ( int i = 0 ; i < N; i++) {
int New_Value = newValue(A[i], M);
v.add( new pair(New_Value, A[i]));
}
Collections.sort(v, new comp());
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] + " " );
}
}
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 };
sortArray(N, A, M);
}
}
class pair{
Integer x;
Integer y;
pair( int x, int y){
this .x = x;
this .y = y;
}
}
class comp implements Comparator<pair>{
public int compare(pair o1, pair o2){
return o1.x.compareTo(o2.x);
}
}
|
Python3
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
def sortArray(N, A, M):
v = []
for i in range (N):
New_Value = newValue(A[i], M)
v.append([New_Value,A[i]])
v.sort(key = cmp_to_key(mycmp))
for i in range (N):
A[i] = v[i][ 1 ]
for i in range (N):
print (A[i],end = " " )
N = 3
A = [ 991 , 338 , 38 ]
M = [ 8 , 9 , 4 , 0 , 2 , 1 , 3 , 5 , 7 , 6 ]
sortArray(N, A, M)
|
C#
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
class GFG2 : IComparer<List< int >>
{
public int Compare(List< int > x, List< int > y)
{
return x.ElementAt(0).CompareTo(y.ElementAt(0));
}
}
class GFG1 {
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;
}
public static void sortArray( int N, int [] A, int [] M)
{
List<List< int >> v = new List<List< int >>();
for ( int i = 0; i < N; i++) {
int New_Value = newValue(A[i], M);
v.Add( new List< int > { New_Value, A[i]});
}
GFG2 gg = new GFG2();
v.Sort(gg);
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 };
sortArray(N, A, M);
}
}
|
Javascript
<script>
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 sortArray(N, A, M)
{
let v = [];
for (let i = 0; i < N; i++) {
let New_Value = newValue(A[i], M);
v.push({ first: New_Value, second: A[i] });
}
v.sort( function (a, b) { return a.first - b.first })
for (let i = 0; i < N; i++) {
A[i] = v[i].second;
}
for (let i = 0; i < N; i++) {
document.write(A[i] + " " );
}
}
let N = 3;
let A = [991, 338, 38];
let M = [8, 9, 4, 0, 2, 1, 3, 5, 7, 6];
sortArray(N, A, M);
</script>
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(N)
Last Updated :
15 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...