All problems
0304EasyMathSimulationNumber Theory

Single Digit Label Check

Tracked in this browser only
Write code

Trains the technique from

LeetCode 258Add Digits

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

Every parcel that leaves the depot carries a tracking number num, and the printer stamps a one-digit check mark next to it.

The check mark is produced like this: add up the digits of num. If that total is still two digits or longer, add up its digits as well, and keep folding the number that way until only one digit is left. That surviving digit is the check mark.

Given num, return its check mark.

Examples

Example 1

Input
num = 4917
Output
3

4 + 9 + 1 + 7 = 21, and 2 + 1 = 3. Three is a single digit, so the check mark is 3.

Example 2

Input
num = 99
Output
9

9 + 9 = 18, then 1 + 8 = 9.

Example 3

Input
num = 12345
Output
6

The digits total 15, and 1 + 5 = 6.

Example 4

Input
num = 2147483647
Output
1

The ten digits total 46, then 4 + 6 = 10, then 1 + 0 = 1. This is the largest tracking number the constraints allow.

Constraints

  • 0 <= num <= 2^31 - 1

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def add_digits(num: int) -> int:
Java
public int addDigits(int num)
September 7
Apply