Four Divisors

Given an integer array nums, return the sum of the divisors of the integers in that array that have exactly four divisors. If no integer in the array has exactly four divisors, return 0.

30:00

Four Divisors
medium
Free written solution
Topics
Companies

Given an integer array nums, return the sum of the divisors of the integers in that array that have exactly four divisors. If no integer in the array has exactly four divisors, return 0.

Example 1:
Input: {"nums":[21,4,7]}
Output: 32
Constraints:
  • 1≤nums.length≤1041 \leq \text{nums.length} \leq 10^4

  • 1≤nums[i]≤1051 \leq \text{nums}[i] \leq 10^5

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[21,4,7]}

For each number, list its divisors in pairs (d, num / d) with d up to the square root. Add the divisor sum only when there are exactly four.

nums (green = exactly four divisors, with the sum it adds)

21

4

7

Divisors come in pairs d and num / d, so testing d up to the square root finds them all.

total

0

Variables
VariableValue
num-
count0
div_sum0
total0
DepthFunction Call
Stack empty
0/12