Magic Number Range Counter
Updated
Problem statement
A positive integer is magic if its decimal digits include each of 2, 3, 5 and 7 at least once. Other digits may appear too, and digits may repeat. For example, 22357 and 73052 are magic, but 2350 is not, because it has no 7.
Given integers L and R, return the number of magic integers x with L <= x <= R. The answer can be larger than a 32-bit integer can hold, so return it as a 64-bit integer.
Function
solve(L: long, R: long) → longExamples
Example 1
L = 10077R = 24391return = 98Every number in this range has five digits and starts with 1 or 2.
- Starting with
1: the other four digits must be exactly2,3,5and7in some order. That gives4! = 24numbers, from12357to17532, all inside the range. - Starting with
2: the last four digits must contain3,5and7, and the number must be at most24391. Look at the second digit:0,1or2: the last three digits are3,5and7in some order, so3 × 6 = 18numbers.3: the last three digits must contain5and7. If the third of them is one of the8digits other than5and7, the three different digits can be arranged in6orders:8 × 6 = 48numbers. If it repeats5or7,557and577each have3orders:6numbers. That is54numbers, all below24000.4: the last three digits are3,5and7in some order, and only24357and24375are at most24391:2numbers.5or more: the number is larger than24391.
In total 24 + 18 + 54 + 2 = 98.
Example 2
L = 22350R = 22380return = 2Every number in this range starts with 223, so it already has a 2 and a 3. It is magic exactly when its last two digits are 5 and 7, in either order. That gives 22357 and 22375, and both are in the range, so the answer is 2. The digit 2 may appear more than once.
Example 3
L = 1R = 1000000000000000000return = 50129575543392204010^18 itself has only the digits 1 and 0, so count the magic numbers below it. Write each of them with exactly 18 digits by adding leading zeros: the added zeros are never 2, 3, 5 or 7, so a number is magic exactly when its 18-digit string contains all four of these digits.
There are 10^18 strings. By inclusion-exclusion, remove those missing one chosen digit (4 × 9^18), add back those missing two (6 × 8^18), remove those missing three (4 × 7^18) and add back those missing all four (6^18). The result, 501295755433922040, needs a 64-bit integer.
Constraints
1 <= L <= R <= 10^18
More Graviton Online Assessment questions
Similar questions at other companies
- Count Nondecreasing Digit-Sum ArraysMicrosoft · OAMicrosoft interview questions
- Stable Workload Redistribution TreesTower Research Capital · OA
- Count Valid A-B-C Sequences Under a Modulo-Four RuleInfosys · OAInfosys interview questions
- Count Permutations Without Three Equal Parities in a RowGoogle · OAGoogle interview questions
- Enough Muffins for the LineSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Count Shipment SchedulesMorgan Stanley · OAMorgan Stanley interview questions
- Find Encrypted NumberAmazon · OA
- Valid PasswordsMathWorks · OAMathWorks interview questions
- Coloring HousesSnowflake · OASnowflake interview questions
- Find Maximum Number of StringsMoveworks · OAMoveworks interview questions