You are given three positive integers num1, num2, and num3.
The key of num1, num2, and num3 is defined as a four-digit number such that:
ith digit (1 <= i <= 4) of the key is generated by taking the smallest digit among the ith digits of num1, num2, and num3.Return the key of the three numbers without leading zeros (if any).
Example 1:
Input: num1 = 1, num2 = 10, num3 = 1000
Output: 0
Explanation:
On padding, num1 becomes "0001", num2 becomes "0010", and num3 remains "1000".
1st digit of the key is min(0, 0, 1).2nd digit of the key is min(0, 0, 0).3rd digit of the key is min(0, 1, 0).4th digit of the key is min(1, 0, 0).Hence, the key is "0000", i.e. 0.
Example 2:
Input: num1 = 987, num2 = 879, num3 = 798
Output: 777
Example 3:
Input: num1 = 1, num2 = 2, num3 = 3
Output: 1
Constraints:
1 <= num1, num2, num3 <= 9999Problem Overview: You receive three integers. Treat each as a 4-digit number (pad with leading zeros if necessary). For every digit position (thousands, hundreds, tens, ones), pick the smallest digit among the three numbers. Combine those minimum digits to form the final 4-digit key.
Approach 1: Basic Approach with String Manipulation (O(d) time, O(d) space)
This method converts each number into a string and ensures it has exactly four characters using leading zeros. You iterate from index 0 to 3, compare the digits from the three strings at the same position, and select the minimum. Append the smallest digit to a result string and convert the final string back to an integer.
The key insight is that digit positions must align. Converting to strings makes this trivial because indexing directly represents each place value. This approach is straightforward to implement and easy to debug, which is why many developers prefer it in interviews when clarity matters.
Approach 2: Bitwise Operations and Mathematical Approach (O(d) time, O(1) space)
This version avoids strings entirely and works directly with digits using arithmetic operations. Repeatedly extract the last digit of each number using % 10. Compute the minimum among those digits, then build the result number using positional multiplication. After processing a digit, remove it from each number using integer division / 10.
You repeat this process four times (since the key always has four digits). The result is constructed by multiplying the chosen minimum digit with the correct place value (1, 10, 100, 1000). This approach uses constant memory and relies purely on numeric operations from math. It is typically faster in low-level implementations because it avoids string allocation.
Even though the platform labels this as a bitwise-style approach, the core idea is digit extraction through arithmetic. It demonstrates strong understanding of number manipulation, which often appears in math and bit manipulation style problems.
Recommended for interviews: The mathematical digit extraction approach is usually preferred. It uses constant extra space and shows that you understand how numbers are represented digit-by-digit. The string method still demonstrates correct reasoning and is perfectly acceptable for an easy problem, especially if the focus is readability rather than micro-optimizations. Understanding both approaches helps when similar digit-processing problems appear in string or numeric manipulation tasks.
In this approach, the main idea is to convert each of the numbers into a four-digit string, padded with leading zeros if needed. Then, by iterating over corresponding digits of each number, we can compute the result by taking the minimum digit at each position. Finally, any leading zeros from the result should be trimmed to adhere to the output requirements.
The solution uses the C standard library function sprintf to convert integers into zero-padded four-digit strings. It then computes the minimum digit for each of the four positions, constructs a new key as a string, and trims leading zeros before returning the result. Using static for the returned string ensures persistence outside the function.
Time Complexity: O(1) because we are iterating through fixed 4 digits.
Space Complexity: O(1) as we use a constant amount of extra space.
This approach is based on handling the digits through bitwise operations and direct mathematical calculations without converting numbers to strings. This helps in environments where low-level operations are preferred. The idea is to extract individual digits from each number, compare them, and construct the final key purely with arithmetic operations.
This solution directly extracts each digit using division and modulo operations without converting the numbers to strings. By iterating over predefined factors, each digit is directly computed, minimizing the necessity for string manipulations and thus improving efficiency and clarity.
Time Complexity: O(1).
Space Complexity: O(1).
We can directly simulate this process by defining a variable ans to store the answer and a variable k to represent the current digit place, where k = 1 represents the units place, k = 10 represents the tens place, and so on.
Starting from the units place, for each digit place, we calculate the current digit of num1, num2, and num3, take the minimum of the three, and then add this minimum value multiplied by k to the answer. Then, multiply k by 10 and continue to the next digit place.
Finally, return the answer.
The time complexity is O(1), and the space complexity is O(1).
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Basic Approach with String Manipulation | Time Complexity: O(1) because we are iterating through fixed 4 digits. |
| Bitwise Operations and Mathematical Approach | Time Complexity: O(1). |
| Simulation | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| String Manipulation | O(d) | O(d) | Best for readability and quick implementation using string indexing |
| Mathematical Digit Extraction | O(d) | O(1) | Preferred when optimizing memory and working directly with digits |
Find the Key of the Numbers || LeetCode Biweekly Contest 138 || Leetcode Solution • codi • 347 views views
Watch 6 more video solutions →Practice Find the Key of the Numbers with our built-in code editor and test cases.
Practice on FleetCode