Find the minimum number possible by changing at most one digit
Last Updated :
05 Jul, 2021
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.
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++
#include <bits/stdc++.h>
using namespace std;
int minimum69Number( int num)
{
string s_num = to_string(num);
for ( auto & c : s_num) {
if (c == '9' ) {
c = '6' ;
break ;
}
}
int result = stoi(s_num);
return result;
}
int main()
{
int n = 9996;
int result = minimum69Number(n);
cout << result << endl;
}
|
Java
class GFG{
static int minimum69Number( int num)
{
char []s_num = String.valueOf(num).toCharArray();
for ( int i = 0 ; i < s_num.length; i++)
{
if (s_num[i] == '9' )
{
s_num[i] = '6' ;
break ;
}
}
int result = Integer.valueOf(String.valueOf(s_num));
return result;
}
public static void main(String[] args)
{
int n = 9996 ;
int result = minimum69Number(n);
System.out.print(result + "\n" );
}
}
|
Python3
def minimum69Number(num):
s_num = str (num)
s_num = s_num.replace( '9' , '6' , 1 )
result = int (s_num)
return result
if __name__ = = '__main__' :
n = 9996
result = minimum69Number(n)
print (result)
|
C#
using System;
class GFG{
static int minimum69Number( int num)
{
char []s_num = String.Join( "" , num).ToCharArray();
for ( int i = 0; i < s_num.Length; i++)
{
if (s_num[i] == '9' )
{
s_num[i] = '6' ;
break ;
}
}
int result = Int32.Parse(String.Join( "" , s_num));
return result;
}
public static void Main(String[] args)
{
int n = 9996;
int result = minimum69Number(n);
Console.Write(result + "\n" );
}
}
|
Javascript
<script>
function minimum69Number(num)
{
let s_num = (num.toString()).split( '' );
for (let i = 0; i < s_num.length; i++)
{
if (s_num[i] == '9' )
{
s_num[i] = '6' ;
break ;
}
}
let result = parseInt(s_num.join( "" ));
return result;
}
let n = 9996;
let result = minimum69Number(n);
document.write(result);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...