Sum of Squares of Special Elements

You are given a 1-indexed integer array nums of length n. An element nums[i] of nums is called special if i divides n, i.e. n % i == 0. Return the sum of the squares of all special elements of nums.

30:00

Sum of Squares of Special Elements
easy
Free written solution
Topics
Companies

You are given a 1-indexed integer array nums of length n. An element nums[i] of nums is called special if i divides n, i.e. n % i == 0. Return the sum of the squares of all special elements of nums.

Example 1:
Input: {"nums":[1,2,3,4]}
Output: 21
Constraints:
  • 1≤nums.length==n≤501 \leq \text{nums.length} == n \leq 50

  • 1≤nums[i]≤501 \leq \text{nums}[i] \leq 50

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

Run code to see results.

Input
arr ={"nums":[1,2,3,4]}

n = 4. A position i (counting from 1) is special when it divides n evenly, so only divisors of 4 count.

special positions are the divisors of n = 4

nums (1-indexed)

1

1

2

2

3

3

4

4

squares of special elements

none yet

total

0

Variables
VariableValue
n4
total0
DepthFunction Call
Stack empty
0/8