Maximum possible number with the given operation
Given a positive integer N, the task is to convert this integer to the maximum possible integer without leading zeroes by changing the digits. A digit X can only be changed into a digit Y if X + Y = 9.
Examples:
Input: N = 42
Output: 57
Change 4 -> 5 and 2 -> 7.
Input: N = 1
Output: 8
Approach: Only the digits which are greater than or equal to 5 need to be changed as changing the digits which are less than 5 will result in a larger number. After all the required digits have been updated, check whether the resultant number has a leading zero, if yes then change it to a 9.
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 maximum possible // integer that can be obtained from the // given integer after performing // the given operations string maxInt(string str) { // For every digit for (int i = 0; i < str.length(); i++) { // Digits greater than or equal to 5 // need not to be changed as changing // them will lead to a smaller number if (str[i] < '5') { str[i] = ('9' - str[i]) + '0'; } } // The resulting integer // cannot have leading zero if (str[0] == '0') str[0] = '9'; return str; } // Driver code int main() { string str = "42"; cout << maxInt(str); return 0; } |
Java
// Java implementation of the approach class GFG { // Function to return the maximum possible // integer that can be obtained from the // given integer after performing // the given operations static String maxInt(char str[]) { // For every digit for (int i = 0; i < str.length; i++) { // Digits greater than or equal to 5 // need not to be changed as changing // them will lead to a smaller number if (str[i] < '5') { str[i] = (char)(('9' - str[i]) + '0'); } } // The resulting integer // cannot have leading zero if (str[0] == '0') str[0] = '9'; String str2 = new String(str); return str2; } // Driver code public static void main (String[] args) { String str = "42"; System.out.println(maxInt(str.toCharArray())); } } // This code is contributed by AnkitRai01 |
Python3
# Python3 implementation of the approach # Function to return the maximum possible # integer that can be obtained from the # given integer after performing # the given operations def maxInt(string): string2 = "" # For every digit for i in range(0, len(string)): # Digits greater than or equal to 5 # need not to be changed as changing # them will lead to a smaller number if (string[i] < '5'): string2 += str((ord('9') - ord(string[i]))) else: string2 += str(string[i]) # The resulting integer # cannot have leading zero if (string2[0] == '0'): string2[0] = '9' return string2 # Driver code if __name__ == '__main__': string = "42" print(maxInt(string)) # This code is contributed by ashutosh450 |
C#
// C# implementation of the approach using System; class GFG { // Function to return the maximum possible // integer that can be obtained from the // given integer after performing // the given operations static String maxInt(char []str) { // For every digit for (int i = 0; i < str.Length; i++) { // Digits greater than or equal to 5 // need not to be changed as changing // them will lead to a smaller number if (str[i] < '5') { str[i] = (char)(('9' - str[i]) + '0'); } } // The resulting integer // cannot have leading zero if (str[0] == '0') str[0] = '9'; String str2 = new String(str); return str2; } // Driver code public static void Main (String []args) { String str = "42"; Console.WriteLine(maxInt(str.ToCharArray())); } } // This code is contributed by Arnab Kundu |
57
Recommended Posts:
- Minimum possible number with the given operation
- Minimum number of given operation required to convert n to m
- Count the total number of triangles after Nth operation
- Form the smallest number using at most one swap operation
- Largest smaller number possible using only one swap operation
- Next higher number using atmost one swap operation
- Check if X and Y can be made zero by using given operation any number of times
- Form the largest number using at most one swap operation
- Check if the given array can be reduced to zeros with the given operation performed given number of times
- Largest number dividing maximum number of elements in the array
- Find the maximum number of composite summands of a number
- Querying maximum number of divisors that a number in a given range has
- Find maximum number that can be formed using digits of a given number
- Number with maximum number of prime factors
- Number of subsets whose mean is maximum
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.

