Leetcode Problem 1390. Four Divisors

1390. Four Divisors

Leetcode Solutions

Optimized Divisor Counting

  1. Initialize result to 0 to store the sum of divisors.
  2. Iterate over each number in the input array nums.
  3. For each number, initialize divisorSum to the number plus 1 (since 1 and the number itself are always divisors).
  4. Initialize divisorCount to 2 (for 1 and the number itself).
  5. Iterate from 2 to the square root of the number.
  6. If the current number is divisible by the iterator i, increment divisorCount by 2 and add i and the quotient to divisorSum.
  7. If divisorCount exceeds 4, break the loop as we are only interested in numbers with exactly four divisors.
  8. If divisorCount is exactly 4 after the loop, add divisorSum to result.
  9. Return result.
UML Thumbnail

Brute Force Divisor Counting

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...