Given an integer array nums
, return the sum of divisors of the integers in that array that have exactly four divisors. If there is no such integer in the array, return 0
.
Example 1:
Input: nums = [21,4,7] Output: 32 Explanation: 21 has 4 divisors: 1, 3, 7, 21 4 has 3 divisors: 1, 2, 4 7 has 2 divisors: 1, 7 The answer is the sum of divisors of 21 only.
Example 2:
Input: nums = [21,21] Output: 64
Example 3:
Input: nums = [1,2,3,4,5] Output: 0
Constraints:
1 <= nums.length <= 104
1 <= nums[i] <= 105
When 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 brute force approach to finding numbers with exactly four divisors means we'll examine each number individually. For each number, we will methodically check all possible numbers to see if they divide evenly into it. Finally, we will determine if it indeed has exactly four of these divisors.
Here's how the algorithm would work step-by-step:
def sum_of_numbers_with_four_divisors(numbers):
final_sum = 0
for current_number in numbers:
list_of_divisors = []
# Find all divisors of the current number
for i in range(1, current_number + 1):
if current_number % i == 0:
list_of_divisors.append(i)
# Check if the current number has exactly four divisors
if len(list_of_divisors) == 4:
# Sum the divisors if the count is four
divisor_sum = sum(list_of_divisors)
# Add the sum to the final sum
final_sum += divisor_sum
return final_sum
The key to efficiently finding numbers with exactly four divisors is understanding how divisors work. A number has exactly four divisors if it's either the product of two distinct prime numbers or the cube of a prime number.
Here's how the algorithm would work step-by-step:
def sumFourDivisors(numbers):
total_sum_of_divisors = 0
def isPrime(number_to_check):
if number_to_check <= 1:
return False
for i in range(2, int(number_to_check**0.5) + 1):
if number_to_check % i == 0:
return False
return True
for number in numbers:
if number < 6:
continue
divisors = []
first_prime_factor = -1
# Finding the first prime factor is a key optimization
for i in range(2, int(number**0.5) + 1):
if number % i == 0:
if isPrime(i):
first_prime_factor = i
break
if first_prime_factor != -1:
second_factor = number // first_prime_factor
# Check for product of two distinct primes
if isPrime(second_factor) and second_factor != first_prime_factor:
divisors = [1, first_prime_factor, second_factor, number]
total_sum_of_divisors += sum(divisors)
else:
# Verify if it could be a cube of a prime
cube_root = round(number ** (1/3))
if cube_root ** 3 == number and isPrime(cube_root):
divisors = [1, cube_root, cube_root * cube_root, number]
total_sum_of_divisors += sum(divisors)
return total_sum_of_divisors
Case | How to Handle |
---|---|
Null or empty input array | Return 0 immediately, as there are no numbers to process. |
Array containing numbers less than 1 (0 or negative numbers) | Ignore these numbers in the divisor calculation, as they cannot have exactly four divisors. |
Large numbers in the input array exceeding integer limits for divisor sum | Use a 64-bit integer type (long) for accumulating the divisor sum to prevent overflow. |
Numbers in the array that are perfect cubes of primes (e.g., 8 = 2^3) | The number of divisors is 4 (1, p, p^2, p^3), so calculate and include the sum of the divisors. |
Numbers in the array that are the product of two distinct primes (e.g., 6 = 2*3) | The number of divisors is 4 (1, p, q, pq), so calculate and include the sum of the divisors. |
Numbers in the array that have more than four divisors | Ensure the divisor counting logic correctly identifies and excludes these numbers. |
Array with a large number of elements, impacting time complexity | Optimize divisor calculation to avoid unnecessary iterations, potentially up to the square root of the number. |
Integer overflow during divisor product | Use long or appropriate data type to handle potential overflows in divisor product calculations. |