Remove one digit 5 from an integer to get the largest value

Quick Overview

Remove exactly one digit 5 from a non-negative integer so that the remaining number is as large as possible. It tests greedy reasoning on digit strings, a clean argument for which 5 to delete, and edge cases such as leading zeros and a single-digit input.

Remove one digit 5 from an integer to get the largest value

Company: DRW

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a non-negative integer `n` that contains at least one digit `5`, remove **exactly one occurrence** of the digit `5` from its decimal representation so that the resulting number is as large as possible. Return the maximum possible integer after the deletion. The result must not contain leading zeros, unless the result itself is zero. ### Function Signature ```python def delete_five_for_max(n: int) -> int: ``` ### Rules - Exactly one `5` is deleted. No other digit is removed, added or moved. - The return value is an integer, so any leading zeros left after the deletion are dropped. Deleting the `5` from `5072` gives `72`. - If `n` is the single digit `5`, nothing is left after the deletion and the result is `0`. ### Constraints - `0 <= n <= 10^18` - `n` contains at least one occurrence of the digit `5` in its decimal representation. ### Examples ```text Input: n = 15958 Output: 1958 ``` The digits are 1, 5, 9, 5, 8. Deleting the first `5` gives 1958 and deleting the second gives 1598, so the answer is 1958. ```text Input: n = 50515 Output: 5051 ``` The three possible deletions give 0515 (which is 515), 5015 and 5051. The largest is 5051. ```text Input: n = 5 Output: 0 ``` The only digit is `5`. Removing it leaves an empty string, which represents 0.

Overview: Remove exactly one digit 5 from a non-negative integer so that the remaining number is as large as possible. It tests greedy reasoning on digit strings, a clean argument for which 5 to delete, and edge cases such as leading zeros and a single-digit input.

DRW logo
DRW
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a non-negative integer n that contains at least one digit 5, remove exactly one occurrence of the digit 5 from its decimal representation so that the resulting number is as large as possible.

Return the maximum possible integer after the deletion. The result must not contain leading zeros, unless the result itself is zero.

Function Signature

def delete_five_for_max(n: int) -> int:

Rules

  • Exactly one 5 is deleted. No other digit is removed, added or moved.
  • The return value is an integer, so any leading zeros left after the deletion are dropped. Deleting the 5 from 5072 gives 72 .
  • If n is the single digit 5 , nothing is left after the deletion and the result is 0 .

Constraints

  • 0 <= n <= 10^18
  • n contains at least one occurrence of the digit 5 in its decimal representation.

Examples

Input: n = 15958
Output: 1958

The digits are 1, 5, 9, 5, 8. Deleting the first 5 gives 1958 and deleting the second gives 1598, so the answer is 1958.

Input: n = 50515
Output: 5051

The three possible deletions give 0515 (which is 515), 5015 and 5051. The largest is 5051.

Input: n = 5
Output: 0

The only digit is 5. Removing it leaves an empty string, which represents 0.