Problema
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.
Vincoli
1 ≤ n ≤ 10¹⁸
Esempi
Input:
n = 352
Output atteso:
expected = 5
Input:
long long n = 352;
Output atteso:
long long expected = 5;
Input:
long n = 352;
Output atteso:
long expected = 5;
Spiegazione
The digits are 3, 5 and 2; the largest is 5.
Input:
n = 7
Output atteso:
expected = 7
Input:
long long n = 7;
Output atteso:
long long expected = 7;
Input:
long n = 7;
Output atteso:
long expected = 7;
Spiegazione
The only digit is 7.
Come affrontarlo
Prova prima da solo, poi apri un suggerimento alla volta.
Suggerimento 1
Compare every decimal digit and keep the biggest one seen so far.
Suggerimento 2
Extract digits one by one from the right, or inspect the string characters.
Suggerimento 3
A number containing 9 anywhere immediately sets the answer to 9.
Complessità attesa
O(d) time, O(1) extra space