Back to blog
Dec 14, 2024
4 min read

Find the Key of the Numbers

You are given three integers. Find the key by taking the maximum digit at each place value among the three numbers.

Difficulty: Easy | Acceptance: 76.50% | Paid: No Topics: Math

You are given three positive integers num1, num2, and num3.

The key of the numbers is defined as the number formed by taking the maximum digit at each place value among the three numbers. For example, if num1 = 123, num2 = 456, and num3 = 789, the key is 789 because 9 is the max in the units place, 8 in the tens, and 7 in the hundreds.

If a number has fewer digits than the others, the missing digits are considered to be 0.

Return the key of the three numbers.

Examples

Example 1:

Input: num1 = 987, num2 = 123, num3 = 456
Output: 987
Explanation:
1s place: max(7, 3, 6) = 7
10s place: max(8, 2, 5) = 8
100s place: max(9, 1, 4) = 9
Result: 987

Example 2:

Input: num1 = 1, num2 = 2, num3 = 3
Output: 3
Explanation:
1s place: max(1, 2, 3) = 3
Result: 3

Example 3:

Input: num1 = 123, num2 = 45, num3 = 6
Output: 146
Explanation:
1s place: max(3, 5, 6) = 6
10s place: max(2, 4, 0) = 4
100s place: max(1, 0, 0) = 1
Result: 146

Constraints

- 1 <= num1, num2, num3 <= 9999

String Manipulation

Intuition Convert the numbers to strings to easily access digits by index. Pad the shorter strings with leading zeros so they all have the same length, then iterate through each position to find the maximum digit.

Steps

  • Convert num1, num2, and num3 to strings.
  • Find the maximum length among the three strings.
  • Pad each string with leading zeros to match the maximum length.
  • Iterate from the most significant digit to the least significant digit.
  • At each index, find the maximum character digit among the three strings.
  • Append the maximum digit to the result string.
  • Convert the result string back to an integer and return it.
python
class Solution:
    def generateKey(self, num1: int, num2: int, num3: int) -&gt; int:
        s1, s2, s3 = str(num1), str(num2), str(num3)
        max_len = max(len(s1), len(s2), len(s3))
        s1 = s1.zfill(max_len)
        s2 = s2.zfill(max_len)
        s3 = s3.zfill(max_len)
        res = []
        for i in range(max_len):
            res.append(str(max(int(s1[i]), int(s2[i]), int(s3[i]))))
        return int("".join(res))

Complexity

  • Time: O(k), where k is the number of digits in the largest number (max 10).
  • Space: O(k) to store the string representations.
  • Notes: String manipulation is often more readable but slightly less efficient than pure math due to memory allocation.

Mathematical Extraction

Intuition Extract digits from the numbers using modulo and division operations. Process the numbers from the least significant digit to the most significant digit, keeping track of the maximum digit at each place value.

Steps

  • Initialize key to 0 and place to 1 (representing the units place).
  • Loop while any of num1, num2, or num3 is greater than 0.
  • Extract the last digit of each number using % 10.
  • Find the maximum of these three digits.
  • Add max_digit * place to the key.
  • Multiply place by 10 to move to the next significant digit.
  • Divide num1, num2, and num3 by 10 to remove the processed digit.
  • Return key.
python
class Solution:
    def generateKey(self, num1: int, num2: int, num3: int) -&gt; int:
        key = 0
        place = 1
        while num1 &gt; 0 or num2 &gt; 0 or num3 &gt; 0:
            d1 = num1 % 10
            d2 = num2 % 10
            d3 = num3 % 10
            max_d = max(d1, d2, d3)
            key += max_d * place
            place *= 10
            num1 //= 10
            num2 //= 10
            num3 //= 10
        return key

Complexity

  • Time: O(k), where k is the number of digits in the largest number.
  • Space: O(1), as we only use a few integer variables.
  • Notes: This is the most optimal approach in terms of space complexity.