You are given a 0-indexed string word of length n consisting of digits, and a positive integer m.
The divisibility array div of word is an integer array of length n such that:
div[i] = 1 if the numeric value of word[0,...,i] is divisible by m, ordiv[i] = 0 otherwise.Return the divisibility array of word.
Example 1:
Input: word = "998244353", m = 3 Output: [1,1,0,0,0,1,1,0,0] Explanation: There are only 4 prefixes that are divisible by 3: "9", "99", "998244", and "9982443".
Example 2:
Input: word = "1010", m = 10 Output: [0,1,0,1] Explanation: There are only 2 prefixes that are divisible by 10: "10", and "1010".
Constraints:
1 <= n <= 105word.length == nword consists of digits from 0 to 91 <= m <= 109When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The goal is to figure out, for each position in a number written as a string, whether the number up to that position is perfectly divisible by a given number. The brute force method involves checking the divisibility at every single position by recalculating the number from the start of the string for each position.
Here's how the algorithm would work step-by-step:
def find_divisibility_array(number_string, divisor):
divisibility_array = []
current_number = 0
for digit_index in range(len(number_string)):
# Rebuild the number from the start of the string.
current_number = int(number_string[:digit_index+1])
# Check if the current number is divisible by the divisor
if current_number % divisor == 0:
divisibility_array.append(1)
else:
divisibility_array.append(0)
return divisibility_arrayWe want to figure out for each part of a number (represented as a string), if that part is divisible by another number. The clever trick is to keep track of the remainder as we move from left to right. We use this remainder to quickly calculate the remainder for the next larger part of the number.
Here's how the algorithm would work step-by-step:
def find_divisibility_array(number_string, divisor):
divisibility_array = []
current_remainder = 0
for digit_char in number_string:
current_digit = int(digit_char)
# Combine the previous remainder with the current digit
combined_number = current_remainder * 10 + current_digit
#Check if the combined number is divisible by the divisor
if combined_number % divisor == 0:
divisibility_array.append(1)
else:
divisibility_array.append(0)
# Calculate the new remainder for the next iteration
current_remainder = combined_number % divisor
return divisibility_array| Case | How to Handle |
|---|---|
| Null or empty input string `word` | Return an empty array or throw an IllegalArgumentException as appropriate for invalid input. |
| Divisor `m` is zero | Throw an ArithmeticException or IllegalArgumentException as division by zero is undefined. |
| Single digit string and small divisor (e.g., "1", m = 2) | The algorithm should correctly calculate and return [1 % m]. |
| Large input string that could cause integer overflow when calculating the prefix modulo m | Use long or BigInteger to avoid overflow when calculating the prefix modulo m. |
| String representing a very large number (e.g., 1000 digits) and a small divisor | Ensure the algorithm's time complexity is linear with the length of the input string, and does not recursively compute the integer value. |
| Divisor `m` is 1 | The resulting divisibility array should be all ones since every number is divisible by 1. |
| Divisor `m` is a very large number, much larger than the accumulated value of the prefix at any point. | The modulus operation should still work correctly, potentially optimizing for the case where the prefix is always less than m. |
| String contains leading zeros | Leading zeros should be treated as part of the number and contribute to the divisibility calculation. |