Maximum 69 Number

Updated on 06 June, 2025
Maximum 69 Number header image

Problem Statement

In this task, you are presented with a non-negative integer called num, which is formed only by the digits 6 and 9. The goal is to derive the maximum possible number from num by altering at most one digit in num. This alteration involves switching a '6' to '9', or vice versa. If no such switch results in a higher number, the number should remain unchanged. The challenge lies not just in switching the digits, but determining where the switch should occur (if at all) to guarantee that num becomes as large as possible.

Examples

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.

Approach and Intuition

  1. The primary goal is to transform the integer num into its maximum form by changing a digit from '6' to '9'. Conversely, transforming '9' to '6' generally reduces the value, which is counterproductive under the task's requirements.

  2. You can approach this problem by:

    • Converting the number to a string or array format for easier manipulation of individual digits.

    • Iteratively examining each digit from the leftmost to the rightmost:

      • If the digit is '6', change it to '9'. Convert the modified string or array back to a number, then compare it with the original number and all previously generated numbers to determine which is largest.
      • If the digit is '9', simply move on to the next digit because switching it to '6' would reduce the number's value.
      • Record the maximum value found during these transformations.
    • Consider the highest value obtained from these comparisons as the final result.

  3. Edge Cases:

    • If the number consists solely of the digit '9', any transformation would only maintain or lower its value. In such scenarios, the original number is already the largest possible.
    • If the number consists solely of the digit '6', converting the first '6' to '9' will yield the largest possible value for the number.

In this manner, the algorithm ensures that the number is increased maximally with minimal transformations, adhering strictly to the constraints and the requirement of changing at most one digit.

Solutions

  • C++
  • Java
cpp
class Solution {
public:
    int highest69Number(int number) {
        int originalNumber = number;
        int positionOfSix = -1;
        int currentPlace = 0;

        while (originalNumber > 0) {
            if (originalNumber % 10 == 6)
                positionOfSix = currentPlace;

            originalNumber /= 10;
            currentPlace++;
        }

        return positionOfSix == -1 ? number : number + 3 * (int)pow(10, positionOfSix);
    }
};

The provided C++ solution is designed to solve the problem titled "Maximum 69 Number". The goal is to convert the maximum possible number of 6s to 9s in a given number to achieve the highest value possible. Here is how the code functions:

  • The function highest69Number begins by initializing two integer variables, originalNumber to store the input number and positionOfSix to track the rightmost position of the digit 6 in the number.
  • A loop runs while originalNumber is greater than 0, where the remainder when originalNumber is divided by 10 is checked. If the remainder is 6, positionOfSix is updated to the current digit position.
  • After examining each digit, originalNumber is divided by 10 to remove the examined digit, and currentPlace is incremented to move to the next position.
  • If no 6 is found (positionOfSix remains -1), the function returns the number as it is. However, if a 6 is found, the code calculates the new number by adding 3 * pow(10, positionOfSix) to the original number. This operation effectively replaces the rightmost 6 with a 9.
  • The use of pow function aids in determining the precise place where the digit needs to be changed from 6 to 9.

This implementation efficiently determines the maximum number achievable by changing a single 6 to a 9, ensuring the change increases the number as much as possible. This approach is straightforward and optimizes the integer calculations without altering or reformatting the entire number, providing an optimal solution for this numerical transformation challenge.

java
class Solution {
    public int maximumNumberByChangingDigits(int inputNumber) {
        int copy = inputNumber;
        int mostSignificantSixIndex = -1;
        int digitPosition = 0;

        while (copy > 0) {
            if (copy % 10 == 6)
                mostSignificantSixIndex = digitPosition;
                
            copy /= 10;
            digitPosition++;
        }

        return mostSignificantSixIndex == -1 ? inputNumber : inputNumber + 3 * (int)Math.pow(10, mostSignificantSixIndex);
    }
}

This Java solution is designed to address the problem of maximizing a number by changing at most one digit from 6 to 9. The given code provides a method, maximumNumberByChangingDigits, which accepts an integer inputNumber and manipulates it to produce the largest possible number under the set constraint (changing one '6' to '9').

Following is how the code operates:

  1. Initialize a copy of the input number, copy, and variables for indexing the digits—mostSignificantSixIndex for tracking the position of the most significant '6', and digitPosition for current digit examination.

  2. Iterate through copy to examine each digit from least significant to most:

    • Check if current digit is '6'. If true, update mostSignificantSixIndex to digitPosition, marking the latest '6' found which could be the most significant when viewed from left-to-right in the number.
    • Reduce copy by a factor of 10 and increment digitPosition for moving to the next digit.
  3. Calculate the result:

    • If mostSignificantSixIndex remains unchanged (i.e., no '6' found), return inputNumber.
    • If a '6' is found, enhance inputNumber by adding 3 multiplied by 10 raised to the power mostSignificantSixIndex. This effectively changes the rightmost '6' into a '9'.

The solution efficiently checks and alters the necessary digit with minimal computational steps, leveraging mathematical operations for digit manipulation without resorting to string conversions, ensuring better performance for larger numbers.

Comments

No comments yet.