Problem
Look at every decimal digit of n and return the smallest one. Zeros inside the number count.
Implement smallest_digit(n).
Input
n: A positive integer.
Output
Return the smallest digit, from 0 to 9.
Constraints
1 ≤ n ≤ 10¹⁸
Examples
Example 1
Input:
n = 352
Expected output:
expected = 2
Input:
long long n = 352;
Expected output:
long long expected = 2;
Input:
long n = 352;
Expected output:
long expected = 2;
Explanation
The digits are 3, 5 and 2; the smallest is 2.
Example 2
Input:
n = 1203
Expected output:
expected = 0
Input:
long long n = 1203;
Expected output:
long long expected = 0;
Input:
long n = 1203;
Expected output:
long expected = 0;
Explanation
The 0 inside the number is the smallest digit.
How to approach it
Try on your own first, then open one hint at a time.
Hint 1
Compare every decimal digit and keep the smallest one seen so far.
Hint 2
Zeros matter here, so do not ignore them while scanning the digits.
Hint 3
Initialize carefully so the first digit always updates the running minimum.
Expected complexity
O(d) time, O(1) extra space