'How to get the maximum possible value obtained by deleting only one digit

I came across this codility test, in which I need to return the maximum possible value obtained by deleting one '5' digit from a number, for example:

  • n = 15958 the function should return 1958
  • n = -1525 return -125
  • n = -50 return 0

I manage to do it by creating a permutation array but it's O(NlogN) can it make it better?



Solution 1:[1]

following MWB answer this handle negative numbers and remove only one digit:

public static void main(String []args){
    int num = -1595;
    int sign = num > 0 ? 1 : -1;
    int value = Math.abs(num);
    int maxValue = Integer.MIN_VALUE;
    String stringValue = Integer.toString(value);

    for (int i = 0; i < stringValue.length(); i++) {

      final String digit = stringValue.substring(i, i + 1);

      if (digit.equals("5")) {
        String stringToCheck = stringValue.substring(0, i) + stringValue.substring(i + 1);
        int intToCheck = Integer.parseInt(stringToCheck) * sign;
        maxValue = Math.max(intToCheck, maxValue);
      }

    }
    System.out.println(maxValue);
}

Solution 2:[2]

All the above answers have a time complexity of O(n^2) if we are considering Java 7 & above. Reason: 1.You are iterating over entire string 2. In each iteration, you are creating a substring which is an O(n)operation in itself

Solution:

int size = String.valueOf(n).length();
if(n<0) {
    size--;
}
int max = Integer.MIN_VALUE;
int divisor = Math.pow(10, size-1);
int left = 0;
int right=0;
while(divisor > 0) {
    left = n/divisor;//left part includes the current 5
    right = n%divisor;
    if(temp%5 == 0 && temp%10 != 0) {
        temp=temp/10;//remove the current 5;
        newNum = temp*divisor+remainder
        max=Math.max(newNum, max);
    }
    divisor = divisor/10;
}
return max;

Solution 3:[3]

int RemoveOneFive(int N)
{
    bool negative = (N>0) ? false:true;
 
    std::string s = std::to_string(N);
    int numFives = 0;
    for(int i=0;i<s.length(); i++)
        numFives = (s[i] == '5') ? numFives + 1 : numFives;
 
    int process = false;
    std::string out;
    for(int i=0;i<s.length(); i++)
    {
        if(s[i] != '5' || process == true)
            out.push_back(s[i]);
 
        else if(s[i] == '5')
        {
            if(numFives == 1)
                continue;
            else
            {
                numFives --;

                if((s[i+1] > '5' && negative == false) || (s[i+1] < '5' && negative == true)
                    process = true;
                else
                    out = out + s[i];
            }
        }
 
    }
 
    return std::atoi(&out[0]);
}

Solution 4:[4]

 public static int MaxAfterRemoving5(int num){
        string numStr = num.ToString();
        List<int> possibilities = new List<int>();
        for (int i = 0; i < numStr.Length;i++){
            if(numStr[i]=='5'){
                string splitStr = numStr.Remove(i, 1);
                possibilities.Add(Convert.ToInt32(splitStr));
            }
        }
        return possibilities.Max();
    }

Solution 5:[5]

Yes, correct, the function will work perfectly with negative and positive numbers too -

public static int getMaxValue(){ int value = -15958;

        int maxValue = Integer.MIN_VALUE;
        String stringValue = Integer.toString(value);
        for (int i = 0; i < stringValue.length(); i++) {
            if(stringValue.charAt(i)=='5') {
                String stringToCheck = stringValue.substring(0, i) + stringValue.substring(i + 1);
                int intToCheck = Integer.parseInt(stringToCheck);
                maxValue = intToCheck > maxValue ? intToCheck : maxValue;
            }
        }
        //System.out.println(maxValue);
    return maxValue;
}

Solution 6:[6]

  • Set MaxValue to Integer.MIN_VALUE

  • Convert to String

  • Iterate over all positions in the String

  • Remove the character

  • Convert the resulting String into an integer

  • Set MaxValue if you found a new max

  • Return the MaxValue after iterating over all values!

    public static void main(String []args){
      int value = 15958;
    
      int maxValue = Integer.MIN_VALUE;
      String stringValue = Integer.toString(value);
      for (int i = 0; i < stringValue.length(); i++) {
          String stringToCheck = stringValue.substring(0, i) + stringValue.substring(i + 1);
          int intToCheck = Integer.parseInt(stringToCheck);
          maxValue = intToCheck > maxValue ? intToCheck : maxValue;
      }
      System.out.println(maxValue);
    }
    

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 daniel gi
Solution 2 wannabeSoftwareEngineer
Solution 3 GAURAV JAIN
Solution 4 Guru316
Solution 5
Solution 6