Advertisement

Four Divisors - LeetCode 1390 Solution

Four Divisors - Complete Solution Guide

Four Divisors is LeetCode problem 1390, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 <= 10 4 1 <= nums[i]

Detailed Explanation

The problem asks us to find the sum of the divisors of all numbers in a given integer array `nums` that have exactly four divisors. If a number in `nums` does not have exactly four divisors, we ignore it. The overall goal is to compute the sum of divisors for numbers that *do* have exactly four divisors and return this sum, or 0 if no such number exists in the input array.

Solution Approach

The solution iterates through the input array `nums`. For each number, it checks if the sum of its divisors has already been computed and stored in a memo. If not, it calculates the divisors. The core idea is to iterate up to the square root of the number and find all divisors. If exactly four divisors are found, their sum is calculated and added to the total sum. The calculated sum (or 0 if the number doesn't have four divisors) is then stored in the memo for future use.

Step-by-Step Algorithm

  1. Step 1: Initialize a memo (hash map) to store the sum of divisors for each number encountered and a total sum variable to 0.
  2. Step 2: Iterate through the input array `nums`.
  3. Step 3: For each number `n`, check if it exists in the memo. If it does, add the stored sum of divisors to the total sum and continue to the next number.
  4. Step 4: If `n` is not in the memo, initialize a set to store its divisors.
  5. Step 5: Iterate from 1 up to the square root of `n`. For each `i`, if `i` is a divisor of `n`, add `i` and `n/i` to the divisors set.
  6. Step 6: While calculating divisors, if the size of the divisor set exceeds 4, break the inner loop as the number can't have exactly four divisors.
  7. Step 7: After the inner loop finishes, check if the divisor set contains exactly four elements.
  8. Step 8: If the divisor set has four elements, calculate the sum of all divisors in the set and add it to the total sum. Store the sum in the memo for the number `n`.
  9. Step 9: If the divisor set does not have four elements, store 0 in the memo for the number `n`.
  10. Step 10: After iterating through all the numbers in `nums`, return the total sum.

Key Insights

  • Insight 1: A number has exactly four divisors if and only if it is either the cube of a prime number (p^3, divisors are 1, p, p^2, p^3) or the product of two distinct prime numbers (p*q, divisors are 1, p, q, p*q).
  • Insight 2: The number of divisors can be determined by iterating only up to the square root of the number. This optimization significantly reduces the computation time.
  • Insight 3: Memoization can be used to store the sum of divisors for previously encountered numbers, improving performance when the same number appears multiple times in the input array.

Complexity Analysis

Time Complexity: O(n*sqrt(m))

Space Complexity: O(n)

Topics

This problem involves: Array, Math.

Companies

Asked at: Capital One.