![]() ![]() Once the number of digits has decreased to 3 digit, it will never go up and stuck with a number below 243.Therefore, it will either cycle or end up with 1, hence there are only two cases we need to handle. Number of digitsĮven for a largest number that has 13 digits, the next number (1053) only has 4 digits then the next one will be lower than 324 since the next number of the largest 4 digits number (9999) will be 324 only.The next one is guaranteed to be lower than 243 because the next number after 999 (the largest number with 3 digits) is 243.From this, we can infer how quickly total digits will dwindle from a very large number to the next. Fortunately, this will never happen.Please notice below table where we enumerate the largest possible number for certain amount of digits and its next number. However, there might be Third case where the number will keep increasing and reaching infinity. Second, the number will eventually enter the loop and cycle back to its earlier number to form a loop. First, the number will eventually reach 1. Based on samples above, we know that there are two possibilities that might happen. ![]() To solve this kind of problem, it’s better to write down some of the examples. However, 2 is not a happy number because 2 2 = 4, 4 2 = 16, 1 2 + 6 2 = 37, and eventually will cycle back to 4 without ever reaching 1.īelow are some of the examples: Input: 19Įxplanation: 2 -> 4 -> 16 -> 37 -> 58 -> 89 -> 145 -> 42 -> 20 -> 4 Constraints: 1 <= n <= 2 31 - 1 Linked List (Floyd’s Cycle Detection Algorithm)Ī number is said to be happy if when it is replaced by the sum of the square of its digits, it will eventually reach 1.įor example, 13 is a happy number because 1 2 + 3 2 = 10, then 1 2 + 0 2 = 1. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |