# Minimum possible number with the given operation

Given a positive integer N, the task is to convert this integer to the minimum 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 = 589
Output: 410
Change 5 -> 4, 8 -> 1 and 9 -> 0

Input: N = 934
Output: 934
934 cannot be minimised.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 ` `using` `namespace` `std; ` ` `  `// Function to return the minimum possible ` `// integer that can be obtained from the ` `// given integer after performing ` `// the given operations ` `string minInt(string str) ` `{ ` `    ``// For every digit ` `    ``for` `(``int` `i = 0; i < str.length(); i++) { ` ` `  `        ``// Digits less than 5 need not to be ` `        ``// changed as changing them will ` `        ``// lead to a larger number ` `        ``if` `(str[i] >= ``'5'``) { ` `            ``str[i] = (``'9'` `- str[i]) + ``'0'``; ` `        ``} ` `    ``} ` ` `  `    ``// The resulting integer ` `    ``// cannot have leading zero ` `    ``if` `(str == ``'0'``) ` `        ``str = ``'9'``; ` ` `  `    ``return` `str; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``string str = ``"589"``; ` ` `  `    ``cout << minInt(str); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` ` `  `// Function to return the minimum possible ` `// integer that can be obtained from the ` `// given integer after performing ` `// the given operations ` ` `  `import` `java.util.*; ` ` `  `class` `GFG{ ` ` `  `static` `String minInt(String str) ` `{ ` `    ``// For every digit ` `    ``String s = ``""``; ` `    ``for` `(``int` `i = ``0``; i < str.length(); i++) ` `    ``{ ` ` `  `        ``// Digits less than 5 need not to be ` `        ``// changed as changing them will ` `        ``// lead to a larger number ` `        ``if` `(str.charAt(i) >= ``'5'``)  ` `        ``{ ` `            ``s += (``char``)((``'9'` `- str.charAt(i)) + ``'0'``); ` `        ``} ` `        ``else` `        ``{ ` `            ``s += str.charAt(i); ` `        ``} ` `         `  `    ``} ` ` `  `    ``// The resulting integer ` `    ``// cannot have leading zero ` `    ``if` `(str.charAt(``0``) == ``'0'``) ` `        ``s += ``'9'``; ` ` `  `    ``return` `s; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String []args) ` `{ ` `    ``String str = ``"589"``; ` ` `  `    ``System.out.println(minInt(str)); ` `} ` `} ` ` `  `// This code is contributed by Surendra_Gangwar `

## Python3

 `# Python3 implementation of the approach ` `  `  `# Function to return the minimum possible ` `# integer that can be obtained from the ` `# given integer after performing ` `# the given operations ` `def` `minInt(str1): ` `     `  `    ``# For every digit ` `    ``for` `i ``in` `range``(``len``(str1)): ` ` `  `        ``# Digits less than 5 need not to be ` `        ``# changed as changing them will ` `        ``# lead to a larger number ` `        ``if` `(str1[i] >``=` `5``): ` `            ``str1[i] ``=` `(``9` `-` `str1[i]) ` ` `  `    ``# The resulting integer ` `    ``# cannot have leading zero ` `    ``if` `(str1[``0``] ``=``=` `0``): ` `        ``str1[``0``] ``=` `9` `         `  `    ``temp ``=` `"" ` ` `  `    ``for` `i ``in` `str1: ` `        ``temp ``+``=` `str``(i) ` ` `  `    ``return` `temp ` ` `  `# Driver code ` `str1 ``=` `"589"` `str1 ``=` `[``int``(i) ``for` `i ``in` `str1] ` ` `  `print``(minInt(str1)) ` ` `  `# This code is contributed by Mohit Kumar `

## C#

 `// C# implementation of the above approach  ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `    ``// Function to return the minimum possible  ` `    ``// integer that can be obtained from the  ` `    ``// given integer after performing  ` `    ``// the given operations  ` `    ``static` `string` `minInt(``char` `[]str)  ` `    ``{  ` `        ``// For every digit  ` `        ``for` `(``int` `i = 0; i < str.Length; i++) ` `        ``{  ` `     `  `            ``// Digits less than 5 need not to be  ` `            ``// changed as changing them will  ` `            ``// lead to a larger number  ` `            ``if` `((``int``)str[i] >= (``int``)(``'5'``))  ` `            ``{  ` `                ``str[i] = (``char``)(((``int``)(``'9'``) -  ` `                                 ``(``int``)(str[i])) +  ` `                                 ``(``int``)(``'0'``));  ` `            ``}  ` `        ``}  ` `     `  `        ``// The resulting integer  ` `        ``// cannot have leading zero  ` `        ``if` `(str == ``'0'``)  ` `            ``str = ``'9'``;  ` `     `  `        ``string` `s = ``new` `string``(str); ` `        ``return` `s;  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``static` `public` `void` `Main () ` `    ``{  ` `        ``string` `str = ``"589"``;  ` `        ``Console.WriteLine(minInt(str.ToCharArray()));  ` `    ``}  ` `} ` ` `  `// This code is contributed by AnkitRai01 `

Output:

```410
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.