Largest possible number by deleting given digit
Find the largest positive integer that can be formed by deleting only one occurrence of a given digit.
Examples:
Input: num = 56321, digit = 5
Output: 6321
Explanation: Since the number 56321 contain only 1 occurrence of 5, we can remove it to get 6321 which is the largest possible positive number.
Input: num = 936230, digit = 3
Output: 96230
Explanation: Since the number 936230 contain 2 occurrences of 3, we can remove either 1st occurrence to get 96230 or remove 2nd occurrence to get 93620. Among the both, 96230 is the largest possible positive number.
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 <bits/stdc++.h>
using namespace std;
string removeX(string N, char X)
{
int index = -1;
for ( int i = 0; i < N.length() - 1; i++) {
if (N[i] == X && N[i] - '0' < N[i + 1] - '0' ) {
index = i;
break ;
}
}
if (index == -1) {
for ( int i = N.length() - 1; i >= 0; i--) {
if (N[i] == X) {
index = i;
break ;
}
}
}
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;
}
|
Java
import java.io.*;
class GFG {
public static String removeX(String N, char X)
{
int index = - 1 ;
for ( int i = 0 ; i < N.length() - 1 ;
i++) {
if (N.charAt(i) == X
&& N.charAt(i) - '0'
< N.charAt(i + 1 ) - '0' ) {
index = i;
break ;
}
}
if (index == - 1 ) {
for ( int i = N.length() - 1 ;
i >= 0 ; i--) {
if (N.charAt(i) == X) {
index = i;
break ;
}
}
}
String ans = "" ;
for ( int i = 0 ; i < N.length(); i++) {
if (i != index)
ans = ans + N.charAt(i);
}
return ans;
}
public static void main(String[] args)
{
String N = "2342" ;
char X = '2' ;
System.out.println(removeX(N, X));
}
}
|
Python3
def removeX(N, X):
index = - 1 ;
for i in range ( len (N) - 1 ):
if (N[i] = = X and ord (N[i]) - ord ( '0' ) < ord (N[i + 1 ]) - ord ( '0' )):
index = i;
break ;
if (index = = - 1 ):
for i in range ( len (N), - 1 , - 1 ):
if (N[i] = = X):
index = i;
break ;
ans = "";
for i in range ( len (N)):
if (i ! = index):
ans = ans + N[i];
return ans;
N = "2342" ;
X = '2' ;
print (removeX(N, X));
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static string removeX( string N, char X)
{
int index = -1;
for ( int i = 0; i < N.Length - 1;
i++) {
if (N[i] == X
&& N[i] - '0'
< N[i + 1] - '0' ) {
index = i;
break ;
}
}
if (index == -1) {
for ( int i = N.Length - 1;
i >= 0; i--) {
if (N[i] == X) {
index = i;
break ;
}
}
}
string ans = "" ;
for ( int i = 0; i < N.Length; i++) {
if (i != index)
ans = ans + N[i];
}
return ans;
}
public static void Main()
{
string N = "2342" ;
char X = '2' ;
Console.Write(removeX(N, X));
}
}
|
Javascript
<script>
function removeX(N, X)
{
let index = -1;
for (let i = 0; i < N.length - 1; i++) {
if (N[i] == X && N[i] - '0' < N[i + 1] - '0' ) {
index = i;
break ;
}
}
if (index == -1) {
for (let i = N.length - 1; i >= 0; i--) {
if (N[i] == X) {
index = i;
break ;
}
}
}
let ans = "" ;
for (let i = 0; i < N.length; i++) {
if (i != index)
ans = ans + N[i];
}
return ans;
}
let N = "2342" ;
let X = '2' ;
document.write(removeX(N, X));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
19 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...