# Largest possible number by deleting given digit

• Last Updated : 25 May, 2022

Given a positive number N, and a digit X, the task is to find the largest possible number by deleting one occurrence of X from N.

Note: The number N will contain at least one digit X

Examples:

Input: N = “325”, X = “5”
Output: “32”
Explanation: There is only one ‘5’ in “325”. After removing ‘5’, the result is “32”.

Input: N = “2342”, X = “2”
Output: “342”
Explanation: We can remove the first ‘2’ to get “342” or remove the second ‘2’ to get “234”.
Since 342 > 234, return “342”.

Approach: The problem can be solved based on the following idea:

To find the maximum number, delete an occurrence in such a position where the next digit is greater than it. Because then the number formed will be large. Such a position should be as close to left as possible for higher values.

Follow the steps mentioned below to implement the idea:

• Remove the leftmost occurrence of X if it’s followed by a larger digit.
• If no occurrence of X is followed by a digit greater than X then remove the last occurrence of the digit.

Below is the implementation of the above approach.

## C++

 `#include ``using` `namespace` `std;` `string removeX(string N, ``char` `X)``{``  ` `    ``// Stores the index of X``    ``// that has to be removed``    ``int` `index = -1;` `    ``// Find leftmost occurrence of X``    ``// such that the digit just after X``    ``// is greater than X``    ``for` `(``int` `i = 0; i < N.length() - 1; i++) {``        ``if` `(N[i] == X && N[i] - ``'0'` `< N[i + 1] - ``'0'``) {` `            ``// Update index and break``            ``index = i;``            ``break``;``        ``}``    ``}` `    ``// If no occurence of X such that``    ``// the digit just after X``    ``// is greater than X is found``    ``// then find last occurence of X``    ``if` `(index == -1) {``        ``for` `(``int` `i = N.length() - 1; i >= 0; i--) {``            ``if` `(N[i] == X) {``                ``index = i;``                ``break``;``            ``}``        ``}``    ``}` `    ``// Construct answer using all characters``    ``// in string N except index``    ``string ans = ``""``;``    ``for` `(``int` `i = 0; i < N.length(); i++) {``        ``if` `(i != index)``            ``ans = ans + N[i];``    ``}` `    ``return` `ans;``}` `int` `main()``{` `    ``string N = ``"2342"``;``    ``char` `X = ``'2'``;``    ``cout << removeX(N, X) << endl;``    ``return` `0;``}` `// This code is contributed by Ishan Khandelwal`

## Java

 `// Java code ot  implement the approach` `import` `java.io.*;` `class` `GFG {``  ` `    ``// Function to find the largest number``    ``public` `static` `String removeX(String N, ``char` `X)``    ``{``        ``// Stores the index of X``        ``// that has to be removed``        ``int` `index = -``1``;` `        ``// Find leftmost occurrence of X``        ``// such that the digit just after X``        ``// is greater than X``        ``for` `(``int` `i = ``0``; i < N.length() - ``1``;``             ``i++) {``            ``if` `(N.charAt(i) == X``                ``&& N.charAt(i) - ``'0'``                    ``< N.charAt(i + ``1``) - ``'0'``) {``                ` `                ``// Update index and break``                ``index = i;``                ``break``;``            ``}``        ``}` `        ``// If no occurence of X such that``        ``// the digit just after X``        ``// is greater than X is found``        ``// then find last occurence of X``        ``if` `(index == -``1``) {``            ``for` `(``int` `i = N.length() - ``1``;``                 ``i >= ``0``; i--) {``                ``if` `(N.charAt(i) == X) {``                    ``index = i;``                    ``break``;``                ``}``            ``}``        ``}` `        ``// Construct answer using all characters``        ``// in string N except index``        ``String ans = ``""``;``        ``for` `(``int` `i = ``0``; i < N.length(); i++) {``            ``if` `(i != index)``                ``ans = ans + N.charAt(i);``        ``}` `        ``return` `ans;``    ``} ``  ` `    ``// Driver code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``String N = ``"2342"``;``        ``char` `X = ``'2'``;``      ` `        ``// Function call``        ``System.out.println(removeX(N, X));``    ``}``}`

## Python3

 `# Python code ot  implement the approach``def` `removeX(N, X):``  ` `    ``# Stores the index of X``    ``# that has to be removed``    ``index ``=` `-``1``;` `    ``# Find leftmost occurrence of X``    ``# such that the digit just after X``    ``# is greater than X``    ``for` `i ``in` `range``(``len``(N) ``-` `1``):``        ``if` `(N[i] ``=``=` `X ``and` `ord``(N[i]) ``-` `ord``(``'0'``) < ``ord``(N[i ``+` `1``]) ``-` `ord``(``'0'``)):` `            ``# Update index and break``            ``index ``=` `i;``            ``break``;` `    ``# If no occurence of X such that``    ``# the digit just after X``    ``# is greater than X is found``    ``# then find last occurence of X``    ``if` `(index ``=``=` `-``1``):``        ``for` `i ``in` `range``(``len``(N), ``-``1``, ``-``1``):``            ``if` `(N[i] ``=``=` `X):``                ``index ``=` `i;``                ``break``;``            `  `    ``# Construct answer using all characters``    ``# in string N except index``    ``ans ``=` `"";``    ``for` `i ``in` `range``(``len``(N)):``        ``if` `(i !``=` `index):``            ``ans ``=` `ans ``+` `N[i];``    `  `    ``return` `ans;` `N ``=` `"2342"``;``X ``=` `'2'``;``print``(removeX(N, X));` `# This code is contributed by Saurabh Jaiswal`

## C#

 `// C# program for the above approach``using` `System;``using` `System.Collections.Generic;` `class` `GFG``{` `  ``// Function to find the largest number``  ``public` `static` `string` `removeX(``string` `N, ``char` `X)``  ``{``    ``// Stores the index of X``    ``// that has to be removed``    ``int` `index = -1;` `    ``// Find leftmost occurrence of X``    ``// such that the digit just after X``    ``// is greater than X``    ``for` `(``int` `i = 0; i < N.Length - 1;``         ``i++) {``      ``if` `(N[i] == X``          ``&& N[i] - ``'0'``          ``< N[i + 1] - ``'0'``) {` `        ``// Update index and break``        ``index = i;``        ``break``;``      ``}``    ``}` `    ``// If no occurence of X such that``    ``// the digit just after X``    ``// is greater than X is found``    ``// then find last occurence of X``    ``if` `(index == -1) {``      ``for` `(``int` `i = N.Length - 1;``           ``i >= 0; i--) {``        ``if` `(N[i] == X) {``          ``index = i;``          ``break``;``        ``}``      ``}``    ``}` `    ``// Construct answer using all characters``    ``// in string N except index``    ``string` `ans = ``""``;``    ``for` `(``int` `i = 0; i < N.Length; i++) {``      ``if` `(i != index)``        ``ans = ans + N[i];``    ``}` `    ``return` `ans;``  ``} ` `  ``// Driver Code``  ``public` `static` `void` `Main()``  ``{``    ``string` `N = ``"2342"``;``    ``char` `X = ``'2'``;` `    ``// Function call``    ``Console.Write(removeX(N, X));``  ``}``}` `// This code is contributed by sanjoy_62.`

## Javascript

 ``
Output
`342`

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up