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 Explanation: There are exactly 3 special elements in nums: nums[1] since 1 divides 4, nums[2] since 2 divides 4, and nums[4] since 4 divides 4. Hence, the sum of the squares of all special elements of nums is nums[1] * nums[1] + nums[2] * nums[2] + nums[4] * nums[4] = 1 * 1 + 2 * 2 + 4 * 4 = 21.
Example 2:
Input: nums = [2,7,1,19,18,3] Output: 63 Explanation: There are exactly 4 special elements in nums: nums[1] since 1 divides 6, nums[2] since 2 divides 6, nums[3] since 3 divides 6, and nums[6] since 6 divides 6. Hence, the sum of the squares of all special elements of nums is nums[1] * nums[1] + nums[2] * nums[2] + nums[3] * nums[3] + nums[6] * nums[6] = 2 * 2 + 7 * 7 + 1 * 1 + 3 * 3 = 63.
Constraints:
1 <= nums.length == n <= 501 <= nums[i] <= 50Problem Overview: Given an array nums of length n, an element is considered special if its 1-indexed position i divides n (i.e., n % i == 0). The task is to square each special element and return the total sum.
Approach 1: Iterating Through Divisors (O(sqrt(n)) time, O(1) space)
The key observation: instead of scanning every index, only positions that divide n can contribute to the result. Iterate through all divisors of n up to sqrt(n). For each divisor d, both d and n/d are valid positions in the array. Convert them to 0-based indices and add the square of the corresponding values. Handle the case where d == n/d to avoid double counting. This reduces unnecessary checks and focuses only on valid positions. The method uses constant extra memory and leverages basic math and divisor enumeration logic.
Approach 2: Using List Comprehension or Direct Enumeration (O(n) time, O(1) extra space)
A straightforward solution iterates through every index from 1 to n. For each position, check if n % i == 0. If true, square nums[i-1] and add it to the running total. In Python or JavaScript, this can be expressed compactly using list comprehension or functional iteration. While slightly less efficient than divisor enumeration for very large arrays, the code is extremely readable and performs well within typical constraints. This approach fits naturally with problems focused on array traversal and simple enumeration.
Recommended for interviews: Start with the direct enumeration approach because it clearly demonstrates you understand the condition n % i == 0. Then mention the divisor-based optimization. Enumerating divisors of n shows stronger algorithmic thinking and reduces the checks from O(n) to O(sqrt(n)), which is the most efficient approach.
This approach involves iterating through all possible indices from 1 to n (inclusive) and checking if the index `i` divides `n` perfectly. If it does, it implies that the element at index `i` is a special element. We then calculate its square and sum up these squares to get the result.
The C solution uses a loop from 1 to numsSize to check divisibility. If i divides numsSize, the element at i-1 (0-indexed) is accessed, squared, and added to sum.
Time Complexity: O(n)
Space Complexity: O(1) because we use only a constant amount of extra space.
Instead of explicitly using a loop, we can employ list comprehension (or equivalent methods) to create a filtered and mapped list of special numbers, square these numbers, and take their sum. This method works well in languages that support quick operations on lists.
This Python solution makes use of a list comprehension to succinctly filter and map the array indices. Only special numbers' squares contribute to the summation.
Python
JavaScript
Time Complexity: O(n)
Space Complexity: O(n) due to list comprehension generating intermediate list.
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Approach 1: Iterating through divisors | Time Complexity: O(n) |
| Approach 2: Using List Comprehension (or Equivalent) | Time Complexity: O(n) |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterating Through Divisors | O(sqrt(n)) | O(1) | Best optimized approach when you want to avoid scanning the entire array |
| Direct Enumeration | O(n) | O(1) | Simplest implementation; good for readability or small arrays |
| List Comprehension (Python/JS) | O(n) | O(1) | Concise functional-style solution in high-level languages |
LeetCode#2778 Sum of Squares of Special Elements - Python • CodeJulian • 1,253 views views
Watch 9 more video solutions →Practice Sum of Squares of Special Elements with our built-in code editor and test cases.
Practice on FleetCode