Find smallest number formed by inverting digits of given number N
Last Updated :
03 Oct, 2022
Given an integer N, the task is to form a minimum possible positive number (>0) by inverting some digits of N.
Inverting for a digit T is defined as subtracting it from 9 that is 9 – T.
Note: The final number should not start from zero.
Examples:
Input:N = 4545
Output: 4444
Explanation:
The minimum possible number is 4444 by subtracting the two 5 ( 9 – 5 = 4)
Input: N = 9000
Output: 9000
Explanation:
The minimum possible number is 9000 cause the number has to be > 0 and hence 9 cannot be subtracted from itself.
Approach: The idea is to iterate over all the digits in the given number and check if 9 – current_digit is less than the current_digit then replace that digit with 9 – current_digit else don’t change the digit. If the first digit of the number is 9 then don’t change the digit and we can’t have a trailing zero in the new number formed.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void number( int num)
{
int a[20], r, i = 0, j;
while (num > 0) {
r = num % 10;
if (9 - r > r)
a[i] = r;
else
a[i] = 9 - r;
i++;
num = num / 10;
}
if (a[i - 1] == 0) {
cout << 9;
i--;
}
for (j = i - 1; j >= 0; j--)
cout << a[j];
}
int main()
{
long long int num = 4545;
number(num);
return 0;
}
|
Java
class GFG{
static void number( int num)
{
int a[] = new int [ 20 ];
int r, i = 0 , j;
while (num > 0 )
{
r = num % 10 ;
if ( 9 - r > r)
a[i] = r;
else
a[i] = 9 - r;
i++;
num = num / 10 ;
}
if (a[i - 1 ] == 0 )
{
System.out.print( "9" );
i--;
}
for (j = i - 1 ; j >= 0 ; j--)
System.out.print(a[j]);
}
public static void main(String []args)
{
int num = 4545 ;
number(num);
}
}
|
Python3
def number(num):
a = [ 0 ] * 20
r, i, j = 0 , 0 , 0
while (num > 0 ):
r = num % 10
if ( 9 - r > r):
a[i] = r
else :
a[i] = 9 - r
i + = 1
num = num / / 10
if (a[i - 1 ] = = 0 ):
print ( 9 , end = "")
i - = 1
for j in range (i - 1 , - 1 , - 1 ):
print (a[j], end = "")
if __name__ = = '__main__' :
num = 4545
number(num)
|
C#
using System;
class GFG{
static void number( int num)
{
int [] a = new int [20];
int r, i = 0, j;
while (num > 0)
{
r = num % 10;
if (9 - r > r)
a[i] = r;
else
a[i] = 9 - r;
i++;
num = num / 10;
}
if (a[i - 1] == 0)
{
Console.Write( "9" );
i--;
}
for (j = i - 1; j >= 0; j--)
Console.Write(a[j]);
}
public static void Main( string []args)
{
int num = 4545;
number(num);
}
}
|
Javascript
<script>
function number(num)
{
let a = new Array(20);
let r, j;
let i = 0;
while (num > 0)
{
r = num % 10;
if (9 - r > r)
a[i] = r;
else
a[i] = 9 - r;
i++;
num = parseInt(num / 10, 10);
}
if (a[i - 1] == 0)
{
document.write(9);
i--;
}
for (j = i - 1; j >= 0; j--)
document.write(a[j]);
}
let num = 4545;
number(num);
</script>
|
Time Complexity: O(log10N)
Auxiliary Space: O(log10N)
Alternate approach -: To get the minimum number we need to convert all digits at their minimum, so we can get the minimum number after subtracting is 4,3,2,1.
Reason-: We can only get 4 or 3 or 2 or 1 or 0 minimals after subtracting any digit from 9 so If we have 5 or 6
or 7 or 8 at the first digit then after subtracting from 9 we can get 4 or 3 or 2 or 1, and if these are not
present then that means that we already have minimum 4 or 3 or 2 or 1 at the first digit.
So we will check for every s[i] if 4<s[i]<9 then we will subtract s[i] from 9 and hence will get the desired result.
C++
#include <bits/stdc++.h>
using namespace std;
string solve(string s)
{
if (s[0] != '9' && s[0] > '4' ) {
s[0] = '0' + ( '9' - s[0]);
}
for ( int i = 1; i < s.size(); i++) {
if (s[i] > '4' )
s[i] = '0' + ( '9' - s[i]);
}
return s;
}
int main()
{
string s = "27" ;
cout << solve(s);
return 0;
};
|
Java
import java.io.*;
class GFG {
static String solve(String s)
{
if (s.charAt( 0 ) != '9' && ( int )s.charAt( 0 ) > ( int ) '4' ) {
s = ( char )(( int ) '0' + (( int ) '9' - ( int )s.charAt( 0 ))) +
s.substring( 1 , s.length());
}
for ( int i = 1 ; i < s.length(); i++) {
if (( int )s.charAt(i) > ( int ) '4' )
s = s.substring( 0 , i) + ( char )(( int ) '0' + (( int ) '9' - ( int )s.charAt(i))) + s.substring(i+ 1 ,s.length());
}
return s;
}
public static void main(String args[])
{
String s = "27" ;
System.out.println(solve(s));
}
}
|
Python3
def solve(s):
if ord (s[ 0 ]) ! = ord ( '9' ) and ord (s[ 0 ]) > ord ( '4' ):
s = s.replace(s[ 0 ] , chr ( ord ( '0' ) + ord ( '9' ) - ord (s[ 0 ])))
for i in range ( 1 , len (s)):
if (s[i] > '4' ):
s = s.replace(s[i] , chr ( ord ( '0' ) + ( ord ( '9' ) - ord (s[i]))))
return s
s = "27"
print (solve(s))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static string solve( string s)
{
if (s[0] != '9' && ( int )s[0] > ( int ) '4' ) {
s = ( char )(( int ) '0' + (( int ) '9' - ( int )s[0]))
+ s.Substring(1, s.Length - 1);
}
for ( int i = 1; i < s.Length; i++) {
if (( int )s[i] > ( int ) '4' )
s = s.Substring(0, i)
+ ( char )(( int ) '0'
+ (( int ) '9' - ( int )s[i]))
+ s.Substring(i + 1, s.Length - i - 1);
}
return s;
}
public static void Main( string [] args)
{
string s = "27" ;
Console.WriteLine(solve(s));
}
}
|
Javascript
<script>
function solve(s)
{
if (s.charCodeAt(0) != '9' .charCodeAt(0) && s.charCodeAt(0) > '4' .charCodeAt(0))
s = s.replace(s[0] , String.fromCharCode( '0' .charCodeAt(0) + '9' .charCodeAt(0) - s.charCodeAt(0)))
for (let i = 1; i < s.length; i++)
{
if (s[i] > '4' )
s = s.replace(s[i] , String.fromCharCode( '0' .charCodeAt(0) + '9' .charCodeAt(0) - s.charCodeAt(i)))
}
return s
}
let s = "27"
document.write(solve(s))
</script>
|
TIME COMPLEXITY – O(N)
SPACE COMPLEXITY – O(1)
Share your thoughts in the comments
Please Login to comment...