You are given a positive integer num consisting only of digits 6 and 9.

Return the maximum number you can get by changing at most one digit (6 becomes 9, and 9 becomes 6).

Example 1:

Input: num = 9669 Output: 9969 Explanation: Changing the first digit results in 6669. Changing the second digit results in 9969. Changing the third digit results in 9699. Changing the fourth digit results in 9666. The maximum number is 9969.

Example 2:

Input: num = 9996 Output: 9999 Explanation: Changing the last digit 6 to 9 results in the maximum number.

Example 3:

Input: num = 9999 Output: 9999 Explanation: It is better not to apply any change.

Constraints:

  • 1 <= num <= 104
  • num consists of only 6 and 9 digits.

Note: This problem is from LeetCode.
❌ Compiled
❌ Executed
❌ Correct
program main
    implicit none

    integer, parameter :: num = 9669
    integer :: max_number

    max_number = solve(num)
    print '(I0)', max_number

contains

    function solve(n) result(max_n)
        implicit none
        integer, intent(in) :: n
        integer :: max_n
        integer :: i, j, k, l

        do i = 1, len(n)
            if (n(i:i) == '6') then
                n(i:i) = '9'
                max_n = max(max_n, n)
                n(i:i) = '6'
            else if (n(i:i) == '9') then
                n(i:i) = '6'
                max_n = max(max_n, n)
                n(i:i) = '9'
            end if
        end do
    end function solve
end program main
❌ Compiled
❌ Executed
❌ Correct
program maxNumber (
    implicit none

    integer :: num, max_num, i
    character(len=1) :: digit

    ! read the input number
    read(*,*) num

    ! initialize the maximum number
    max_num = num

    ! loop through all digits of the number
    do i = 1, len_trim(num)
        digit = num(i:i)

        ! if the digit is a 6, change it to a 9
        if (digit == '6') then
            max_num = transfer(max_num, 9, i)
        end if
    end do

    ! print the maximum number
    write(*,*) max_num

end program maxNumber
🌐 Data from online sources
def maximum69Number(num: int) -> int:
    num_str = list(str(num))
    for i in range(len(num_str)):
        if num_str[i] == '6':
            num_str[i] = '9'
            break
    return int(''.join(num_str))

The algorithm to solve this problem is simple:

  1. Convert the integer num to a string, so that we can easily manipulate individual digits.
  2. Iterate through the string, looking for the first occurrence of the digit '6'.
  3. When the digit '6' is found, change it to '9' and exit the loop.
  4. Convert the modified string back to an integer and return it.

This algorithm guarantees that only one digit will be modified at most, and the result will be the maximum value possible under the given constraints.

🌐 Data from online sources
int maximum69Number (int num) {
    string numStr = to_string(num);
    for (char &c : numStr) {
        if (c == '6') {
            c = '9';
            break;
        }
    }
    return stoi(numStr);
}

The algorithm to solve this problem is simple:

  1. Convert the integer num to a string, so that we can easily manipulate individual digits.
  2. Iterate through the string, looking for the first occurrence of the digit '6'.
  3. When the digit '6' is found, change it to '9' and exit the loop.
  4. Convert the modified string back to an integer and return it.

This algorithm guarantees that only one digit will be modified at most, and the result will be the maximum value possible under the given constraints.