Find smallest number formed by inverting digits of given number N

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 trailing zero in the new number formed.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
#include <iostream>
using namespace std;
 
// Function to invert the digits of
// integer N to form minimum
// possible number
void number(int num)
{
    // Initialize the array
    int a[20], r, i = 0, j;
 
    // Iterate till the nuber N exists
    while (num > 0) {
 
        // Last digit of the number N
        r = num % 10;
 
        // Checking if the digit is
        // smaller than 9-digit
        if (9 - r > r)
 
            // Store the smaller
            // digit in the array
            a[i] = r;
 
        else
            a[i] = 9 - r;
 
        i++;
 
        // Reduce the number each time
        num = num / 10;
    }
 
    // Check if the digit starts
    // with 0 or not
    if (a[i - 1] == 0) {
        cout << 9;
        i--;
    }
 
    // Print the answer
    for (j = i - 1; j >= 0; j--)
        cout << a[j];
}
 
// Driver Code
int main()
{
    // Given Number
    long long int num = 4545;
 
    // Function Call
    number(num);
 
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
class GFG{
  
// Function to invert the digits of
// integer N to form minimum
// possible number
static void number(int num)
{
    // Initialize the array
    int a[] = new int[20];
    int r, i = 0, j;
  
    // Iterate till the nuber N exists
    while (num > 0)
    {
  
        // Last digit of the number N
        r = num % 10;
  
        // Checking if the digit is
        // smaller than 9-digit
        if (9 - r > r)
  
            // Store the smaller
            // digit in the array
            a[i] = r;
  
        else
            a[i] = 9 - r;
  
        i++;
  
        // Reduce the number each time
        num = num / 10;
    }
  
    // Check if the digit starts
    // with 0 or not
    if (a[i - 1] == 0)
    {
        System.out.print("9");
        i--;
    }
  
    // Print the answer
    for (j = i - 1; j >= 0; j--)
        System.out.print(a[j]);
}
  
// Driver Code
public static void main(String []args)
{
    // Given Number
     int num = 4545;
  
    // Function Call
    number(num);
}
}
 
// This code is contributed by rock_cool

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for the above approach
 
# Function to invert the digits of
# integer N to form minimum
# possible number
def number(num):
   
    # Initialize the array
    a = [0] * 20
    r, i, j = 0, 0, 0
 
    # Iterate till the nuber N exists
    while (num > 0):
 
        # Last digit of the number N
        r = num % 10
 
        # Checking if the digit is
        # smaller than 9-digit
        if (9 - r > r):
 
            # Store the smaller
            # digit in the array
            a[i] = r
 
        else:
            a[i] = 9 - r
 
        i += 1
 
        # Reduce the number each time
        num = num // 10
 
    # Check if the digit starts
    # with 0 or not
    if (a[i - 1] == 0):
        print(9, end = "")
        i -= 1
 
    # Prthe answer
    for j in range(i - 1, -1, -1):
        print(a[j], end = "")
 
# Driver Code
if __name__ == '__main__':
   
    # Given Number
    num = 4545
 
    # Function Call
    number(num)
 
# This code is contributed by Mohit Kumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above approach
using System;
class GFG{
   
// Function to invert the digits of
// integer N to form minimum
// possible number
static void number(int num)
{
    // Initialize the array
    int[] a = new int[20];
    int r, i = 0, j;
   
    // Iterate till the nuber N exists
    while (num > 0)
    {
   
        // Last digit of the number N
        r = num % 10;
   
        // Checking if the digit is
        // smaller than 9-digit
        if (9 - r > r)
   
            // Store the smaller
            // digit in the array
            a[i] = r;
   
        else
            a[i] = 9 - r;
   
        i++;
   
        // Reduce the number each time
        num = num / 10;
    }
   
    // Check if the digit starts
    // with 0 or not
    if (a[i - 1] == 0)
    {
        Console.Write("9");
        i--;
    }
   
    // Print the answer
    for (j = i - 1; j >= 0; j--)
        Console.Write(a[j]);
}
   
// Driver Code
public static void Main(string []args)
{
    // Given Number
     int num = 4545;
   
    // Function Call
    number(num);
}
}
  
// This code is contributed by Ritik Bansal

chevron_right


Output: 

4444






 

Time Complexity: O(log10N)
Auxiliary Space: O(log10N)

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.