3270. Find the Key of the Numbers (Leetcode Easy)
Find the Key of the Numbers - Video Solution
Watch 7 video solutions for Find the Key of the Numbers, a easy level problem involving Math. This walkthrough by Programming Live with Larry has 291 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Problem Statement
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:
- Initially, if any number has less than four digits, it is padded with leading zeros.
- The
ithdigit (1 <= i <= 4) of thekeyis generated by taking the smallest digit among theithdigits ofnum1,num2, andnum3.
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".
- The
1stdigit of thekeyismin(0, 0, 1). - The
2nddigit of thekeyismin(0, 0, 0). - The
3rddigit of thekeyismin(0, 1, 0). - The
4thdigit of thekeyismin(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 <= 9999
Approach Overview
Problem 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.
Complexity Analysis
| 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 |