# Find the minimum number possible by changing at most one digit

Given a positive integer N consisting only two types of digits 6 and 9, the task is to generate the minimum number possible by reversing at most one digit, that is 9 becomes 6 or vice versa.

Examples:

Input : 9996
Output : 6996
Explanation :
Changing the first digit results in 6996.
Changing the second digit results in 9696.
Changing the third digit results in 9966.
Changing the fourth digit results in 9999.
Hence, the minimum number among all possibilities is 6996.

Input : 6696
Output : 6666
Explanation :
Changing the first digit results in 9696.
Changing the second digit results in 6996.
Changing the third digit results in 6666.
Changing the fourth digit results in 6699.
Hence, the minimum number among all possibilities is 6666.

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

Approach:
In order to solve the problem, we need to follow the steps given below:

• First convert the given integer into a string.
• Traverse the string from left and change the very first occurrence of ‘9’ to ‘6’ and break out of the loop. If there is no occurrence of 9 in the initial string, then it is already the lowest number possible.
• Convert the final string back to the integer and print it.

Below is the implementation of the above approach:

## C++

 `// C++ implementation to change at most ` `// one digit to make the number ` `// as minimum as possible ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to return the minimum  ` `// possible number ` `int` `minimum69Number(``int` `num) ` `{ ` ` `  `    ``// Converting given number to string ` `    ``string s_num = to_string(num); ` ` `  `    ``// Traversing the string ` `    ``for` `(``auto``& c : s_num) { ` `        ``// change first 9 to 6 ` `        ``if` `(c == ``'9'``) { ` `            ``c = ``'6'``; ` `            ``break``; ` `        ``} ` `    ``} ` ` `  `    ``// Change the string back to the integer ` `    ``int` `result = stoi(s_num); ` ` `  `    ``// Return the final result ` `    ``return` `result; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``// Input number ` `    ``int` `n = 9996; ` ` `  `    ``int` `result = minimum69Number(n); ` ` `  `    ``// Print the result ` `    ``cout << result << endl; ` `} `

## Java

 `// Java implementation to change at most ` `// one digit to make the number ` `// as minimum as possible ` `class` `GFG{ ` ` `  `// Function to return the minimum  ` `// possible number ` `static` `int` `minimum69Number(``int` `num) ` `{ ` ` `  `    ``// Converting given number to String ` `    ``char` `[]s_num = String.valueOf(num).toCharArray(); ` `     `  `    ``// Traversing the String ` `    ``for``(``int` `i = ``0``; i < s_num.length; i++) ` `    ``{ ` ` `  `       ``// change first 9 to 6 ` `       ``if` `(s_num[i] == ``'9'``) ` `       ``{ ` `           ``s_num[i] = ``'6'``; ` `           ``break``; ` `       ``} ` `    ``} ` ` `  `    ``// Change the String back to the integer ` `    ``int` `result = Integer.valueOf(String.valueOf(s_num)); ` ` `  `    ``// Return the final result ` `    ``return` `result; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` `  `    ``// Input number ` `    ``int` `n = ``9996``; ` `    ``int` `result = minimum69Number(n); ` ` `  `    ``// Print the result ` `    ``System.out.print(result + ``"\n"``); ` `} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

## Python3

 `# Python3 implementation to change at most ` `# one digit to make the number ` `# as minimum as possible ` ` `  `# Function to return the minimum  ` `# possible number ` `def` `minimum69Number(num): ` `     `  `    ``# Converting given number to string ` `    ``s_num ``=` `str``(num) ` `     `  `    ``s_num ``=` `s_num.replace(``'9'``,``'6'``, ``1``) ` ` `  `    ``# Change the string back to the integer ` `    ``result ``=` `int``(s_num) ` ` `  `    ``# Return the final result ` `    ``return` `result ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `     `  `    ``# Input number ` `    ``n ``=` `9996` `     `  `    ``result ``=` `minimum69Number(n) ` ` `  `    ``# Print the result ` `    ``print``(result) ` ` `  `# This code is contributed by Samarth `

## C#

 `// C# implementation to change at most ` `// one digit to make the number ` `// as minimum as possible ` `using` `System; ` ` `  `class` `GFG{ ` ` `  `// Function to return the minimum  ` `// possible number ` `static` `int` `minimum69Number(``int` `num) ` `{ ` ` `  `    ``// Converting given number to String ` `    ``char` `[]s_num = String.Join(``""``, num).ToCharArray(); ` `     `  `    ``// Traversing the String ` `    ``for``(``int` `i = 0; i < s_num.Length; i++) ` `    ``{ ` `       ``// change first 9 to 6 ` `       ``if` `(s_num[i] == ``'9'``) ` `       ``{ ` `           ``s_num[i] = ``'6'``; ` `           ``break``; ` `       ``} ` `    ``} ` ` `  `    ``// Change the String back to the integer ` `    ``int` `result = Int32.Parse(String.Join(``""``, s_num)); ` ` `  `    ``// Return the readonly result ` `    ``return` `result; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` `  `    ``// Input number ` `    ``int` `n = 9996; ` `    ``int` `result = minimum69Number(n); ` ` `  `    ``// Print the result ` `    ``Console.Write(result + ``"\n"``); ` `} ` `} ` ` `  `// This code is contributed by sapnasingh4991 `

Output:

```6996
```

Time Complexity: O(n)

Auxiliary Space: O(1) 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.