Problem
Look at every decimal digit of n and return the largest one.
Implement largest_digit(n).
Input
n: A positive integer.
Output
Return the largest digit, from 0 to 9.
Constraints
1 ≤ n ≤ 10¹⁸
Examples
Example 1
Input:
n = 352
Expected output:
expected = 5
Input:
long long n = 352;
Expected output:
long long expected = 5;
Input:
long n = 352;
Expected output:
long expected = 5;
Explanation
The digits are 3, 5 and 2; the largest is 5.
Example 2
Input:
n = 7
Expected output:
expected = 7
Input:
long long n = 7;
Expected output:
long long expected = 7;
Input:
long n = 7;
Expected output:
long expected = 7;
Explanation
The only digit is 7.
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 biggest one seen so far.
Hint 2
Extract digits one by one from the right, or inspect the string characters.
Hint 3
A number containing 9 anywhere immediately sets the answer to 9.
Expected complexity
O(d) time, O(1) extra space