Problem Statement
We start with an integer and create a sequence of successors using the following procedure: First split the decimal representation of the given number into several (at least two) parts, and multiply the parts to get a possible successor. With the selected successor, we repeat this procedure to get a third number, and so on, until we reach a single-digit number.
For example, let's say we start with the number 234. The possible successors are:
- 23 * 4 = 92,
- 2 * 34 = 68 and
- 2 * 3 * 4 = 24.
Given the starting number, start, return the length of the longest sequence that can be generated with this procedure. In the example, the given sequence would be the longest one since the other selections in the first step would give the sequences: (234, 92, 18, 8) and (234, 24, 8), which are both shorter than (234, 68, 48, 32, 6).
Definition
- Class:
- NumberSplit
- Method:
- longestSequence
- Parameters:
- int
- Returns:
- int
- Method signature:
- int longestSequence(int start)
- (be sure your method is public)
Notes
- There can not exist an infinite sequence.
- Although we use no leading zeros in the decimal representation of the number we start with, the parts we get by splitting the number may have leading zeros (e.g. from 3021 we can get 3 * 021 = 63).
Constraints
- start is between 1 and 100000, inclusive.
Examples
6
Returns: 1
A single-digit number is already the last number.
97
Returns: 4
For two-digit numbers, there is always only one possible sequence. Here: 97 -> 63 -> 18 -> 8 (4 numbers).
234
Returns: 5
The example from the problem statement.
876
Returns: 7
Here, it is optimal to make a three way split in the first step - i.e. use 8*7*6=336 as the first successor. Although a two way split would lead to a larger number (87*6=522 or 8*76=608), both these choices would lead in the end to a shorter sequence. The optimal sequence is: 876, 8*7*6=336, 33*6=198, 19*8=152, 1*52=52, 5*2=10, 1*0=0.
99999
Returns: 29
10
Returns: 2
32
Returns: 2
99
Returns: 3
102
Returns: 3
60009
Returns: 4
39
Returns: 4
77
Returns: 5
117
Returns: 6
139
Returns: 7
419
Returns: 8
619
Returns: 9
777
Returns: 10
4
Returns: 1
300
Returns: 2
109
Returns: 3
950
Returns: 4
1604
Returns: 5
841
Returns: 6
776
Returns: 7
796
Returns: 8
4212
Returns: 9
22071
Returns: 10
44255
Returns: 11
33145
Returns: 12
22222
Returns: 13
6896
Returns: 14
90542
Returns: 15
31723
Returns: 16
77875
Returns: 17
29551
Returns: 18
28856
Returns: 19
63526
Returns: 20
41821
Returns: 21
34718
Returns: 22
68606
Returns: 23
89174
Returns: 24
69178
Returns: 25
94816
Returns: 26
99774
Returns: 27
89924
Returns: 28
98994
Returns: 29
99991
Returns: 30
94389
Returns: 31
100000
Returns: 2
97
Returns: 4
9998
Returns: 18
100000
Returns: 2
234
Returns: 5
23579
Returns: 19
99999
Returns: 29
876
Returns: 7