1840. Peak Digit
by Rohit Parihar2 submissions
A display board shows the digits of an account number, and its brightness is set by the largest digit shown. Given the integer n, return the largest decimal digit in it. A minus sign is ignored, so the largest digit of -9 is 9, and the largest digit of 0 is 0.
Do not convert the number to text: take the last digit with the remainder by 10, compare it with the best so far, and remove it with integer division. Aim for O(log n) time and O(1) extra space.
Example 1
- Input:
- n = 40517
- Output:
- 7
- Explanation:
The digits are 4, 0, 5, 1 and 7, and 7 is the largest.
Example 2
- Input:
- n = -9
- Output:
- 9
- Explanation:
The sign is ignored.
Example 3
- Input:
- n = 0
- Output:
- 0
- Explanation:
The only digit is 0.
Constraints
-1015 ≤ n ≤ 1015
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(log n)
- Space
- O(1)