Find the Key of the Numbers - Problem

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 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).

Input & Output

Example 1 — Basic Case
$ Input: num1 = 1, num2 = 22, num3 = 333
Output: 1
💡 Note: Pad to 4 digits: 0001, 0022, 0333. Position-wise minimums: 0,0,0,1 → Key = 0001 → 1
Example 2 — All Same Length
$ Input: num1 = 1234, num2 = 5678, num3 = 9012
Output: 1012
💡 Note: Already 4 digits. Position-wise minimums: min(1,5,9)=1, min(2,6,0)=0, min(3,7,1)=1, min(4,8,2)=2 → 1012
Example 3 — Leading Zeros Result
$ Input: num1 = 987, num2 = 123, num3 = 456
Output: 123
💡 Note: Pad to 4 digits: 0987, 0123, 0456. Position-wise minimums: 0,1,2,3 → Key = 0123 → 123

Constraints

  • 1 ≤ num1, num2, num3 ≤ 9999
  • All three numbers are positive integers

Visualization

Tap to expand
Find the Key of the Numbers INPUT Original Numbers: num1 = 1 num2 = 22 num3 = 333 Padded to 4 digits: 0 0 0 1 num1 0 0 2 2 num2 0 3 3 3 num3 pos1 pos2 pos3 pos4 ALGORITHM STEPS 1 Pad Numbers Add leading zeros to 4 digits 2 Compare Each Position Find min digit at each pos 3 Build Key Combine minimum digits 4 Remove Leading Zeros Return as integer Min at each position: 0,0,0 0 0,0,3 0 0,2,3 0 1,2,3 1 Key with zeros: 0021 Remove leading zeros 0021 --> 21 FINAL RESULT The key of the numbers: 21 Calculation Summary: Position 1: min(0,0,0) = 0 Position 2: min(0,0,3) = 0 Position 3: min(0,2,3) = 0 Position 4: min(1,2,3) = 1 Raw key: 0021 Without leading zeros: 21 Output: 21 Key Insight: The key is formed by taking the MINIMUM digit at each corresponding position across all three numbers. Pad shorter numbers with leading zeros first, then compare digit-by-digit. Time complexity: O(1) for fixed 4 digits. TutorialsPoint - Find the Key of the Numbers | Optimal Solution
Asked in
Google 15 Amazon 12
8.5K Views
Medium Frequency
~8 min Avg. Time
350 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen