Maximize the given number by replacing a segment of digits with the alternate digits given
Given a large number of N digits. We are also given with 10 numbers which represents the alternate number for all the one-digit numbers from 0 to 9. We can replace any digit in N with the given alternate digit to it, but we are only allowed to replace any consecutive segment of numbers for once only, the task is to replace any consecutive segment of numbers such that the obtained number is the largest of all possible replacements.
Examples:
Input: n = 1337, a[] = {0, 1, 2, 5, 4, 6, 6, 3, 1, 9}
Output: 1557
1 can be replaced with 1 as a[1] = 1 (No effect)
3 can be replaced with 5
7 can be replaced with 3 (isn’t required if the number needs to be maximized)
Input: number = 11111, a[] = {0, 5, 2, 5, 4, 6, 6, 3, 1, 9}
Output: 55555
Approach: Since we need to get the largest number possible, hence iterate from the left and find the number whose alternate number is greater than the current one, and keep replacing the upcoming numbers till the alternate number is not smaller.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the maximized number string get_maximum(string s, int a[]) { int n = s.size(); // Iterate till the end of the string for (int i = 0; i < n; i++) { // Check if it is greater or not if (s[i] - '0' < a[s[i] - '0']) { int j = i; // Replace with the alternate till smaller while (j < n && (s[j] - '0' <= a[s[j] - '0'])) { s[j] = '0' + a[s[j] - '0']; j++; } return s; } } // Return original s in case // no change took place return s; } // Driver Code int main() { string s = "1337"; int a[] = { 0, 1, 2, 5, 4, 6, 6, 3, 1, 9 }; cout << get_maximum(s, a); return 0; } |
Java
// Java implementation of the approach class GFG { // Function to return the maximized number static String get_maximum(char[] s, int a[]) { int n = s.length; // Iterate till the end of the string for (int i = 0; i < n; i++) { // Check if it is greater or not if (s[i] - '0' < a[s[i] - '0']) { int j = i; // Replace with the alternate till smaller while (j < n && (s[j] - '0' <= a[s[j] - '0'])) { s[j] = (char) ('0' + a[s[j] - '0']); j++; } return String.valueOf(s); } } // Return original s in case // no change took place return String.valueOf(s); } // Driver Code public static void main(String[] args) { String s = "1337"; int a[] = { 0, 1, 2, 5, 4, 6, 6, 3, 1, 9 }; System.out.println(get_maximum(s.toCharArray(), a)); } } /* This code contributed by PrinciRaj1992 */ |
Python3
# Python3 implementation of the approach # Function to return the maximized number def get_maximum(s, a) : s = list(s) n = len(s) # Iterate till the end of the string for i in range(n) : # Check if it is greater or not if (ord(s[i]) - ord('0') < a[ord(s[i]) - ord('0')]) : j = i # Replace with the alternate till smaller while (j < n and (ord(s[j]) - ord('0') <= a[ord(s[j]) - ord('0')])) : s[j] = chr(ord('0') + a[ord(s[j]) - ord('0')]) j += 1 return "".join(s); # Return original s in case # no change took place return s # Driver Code if __name__ == "__main__" : s = "1337" a = [ 0, 1, 2, 5, 4, 6, 6, 3, 1, 9 ] print(get_maximum(s, a)) # This code is contributed by AnkitRai01 |
C#
// C# implementation of the approach using System; class GFG { // Function to return the maximized number static String get_maximum(char[] s, int[] a) { int n = s.Length; // Iterate till the end of the string for (int i = 0; i < n; i++) { // Check if it is greater or not if (s[i] - '0' < a[s[i] - '0']) { int j = i; // Replace with the alternate till smaller while (j < n && (s[j] - '0' <= a[s[j] - '0'])) { s[j] = (char) ('0' + a[s[j] - '0']); j++; } return String.Join("",s); } } // Return original s in case // no change took place return String.Join("",s); } // Driver Code public static void Main(String[] args) { String s = "1337"; int[] a = { 0, 1, 2, 5, 4, 6, 6, 3, 1, 9 }; Console.WriteLine(get_maximum(s.ToCharArray(), a)); } } // This code contributed by Rajput-Ji |
PHP
<?php // PHP implementation of the approach // Function to return the maximized number function get_maximum($s, $a) { $n = strlen($s); // Iterate till the end of the string for ($i = 0; $i < $n; $i++) { // Check if it is greater or not if ($s[$i] - '0' < $a[$s[$i] - '0']) { $j = $i; // Replace with the alternate till smaller while ($j < $n && ($s[$j] - '0' <= $a[$s[$j] - '0'])) { $s[$j] = '0' + $a[$s[$j] - '0']; $j++; } return $s; } } // Return original s in case // no change took place return $s; } // Driver Code $s = "1337"; $a = array( 0, 1, 2, 5, 4, 6, 6, 3, 1, 9 ); echo get_maximum($s, $a); // This Code is contributed is Tushill. ?> |
1557
Recommended Posts:
- Maximize the value of A by replacing some of its digits with digits of B
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Find the average of k digits from the beginning and l digits from the end of the given number
- Check if the sum of digits of number is divisible by all of its digits
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Sum of the digits of square of the given number which has only 1's as its digits
- Find smallest number with given number of digits and sum of digits
- Find the Largest number with given number of digits and sum of digits
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Number of digits in the nth number made of given four digits
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Number with even sum of digits
- LCM of digits of a given number
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.


