This problem is a favorite warm-up question at companies like Google because it tests your understanding of divisibility, array indexing, and the ability to translate mathematical conditions into clean code. At its core, this is a divisor identification problem disguised as an array traversal challenge.
Problem statement
You are given a 1-indexed integer array nums of length n. Your task is to return the sum of the squares of all special elements in nums.
An element nums[i] is considered special if its indexes i divides n evenly (i.e., n % i == 0).
Constraints:
- 1 <=
nums.length== n <= 50 - 1 <=
nums[i]<= 50
Examples
| Input | Output | Explanation |
[1, 4, 3, 2, 5] | 26 | For n = 5, the special indexes are 1 and 5 as they divide 5, and the sum of their squares is 1² + 5² = 26. |
[2, 4, 17, 18, 21, 18, 26] | 680 | For n = 7, the special indexes are 1, 7, as they divide 7 evenly, and the sum of their corresponding squared values is 2² + 26² = 4 + 676 = 680. |
[5] | 25 | For n = 1, the only special index is 1 as it divides 1, and the sum of squares is 5² = 25. |
Intuition
The key approach to this problem is to iterate over all possible indexes from 1 to n, check whether each index divides n, and, if so, square the element at that index and add it to our running sum.
Step-by-step algorithm
- Get the length of the array and store it as
n. - Initialize a variable
total_sumto 0 to store the cumulative sum of squares. - Iterate through each potential index
ifrom 1 to n + 1.- For each
i, check ifiis a divisor ofn(i.e.,n % i == 0).- If
idividesn, access the element at positioniin the 1-indexed array (which isnums[i - 1]in 0-indexed Python, square this element and add it tototal_sum.
- If
- For each
- After checking all indexes, return
total_sum.
Code implementation
Let’s look at the code for the solution we just discussed.
Code for the Sum of Squares of Special Elements problem
Time complexity
The time complexity of this algorithm is O(n) where n is the length of the input array. We iterate through all indexes from 1 to n exactly once, performing a constant-time modulo operation and array access for each index.
Space complexity
The space complexity is O(1) because we only use a fixed amount of extra space regardless of input size. We maintain only two variables: total_sum (to accumulate the result) and n (to store the array length). We don’t create any additional data structures that scale with the input, and the iteration variable i is reused in each loop iteration. In the worst case, where every index divides n (which only happens when n = 1), we still use constant space.
Common pitfalls and interview tips
- Off-by-one indexing errors: The problem explicitly states the array is 1-indexed, but Python uses 0-indexing. Always remember to access
nums[i - 1]when working with a 1-indexed positioni. A common mistake is to accessnums[i]directly, which will cause an index out of bounds error wheni == n. - Forgetting that n itself is always a divisor: Every positive integer divides itself, so
nums[n](ornums[n - 1]in 0-indexed Python) will always be included in the sum. This is easy to miss during manual calculation, especially with larger arrays. - Misunderstanding the divisibility check: Some candidates mistakenly check if
i % n == 0instead ofn % i == 0. Remember: we’re asking “does index i divide the length n?”, not the reverse. The condition must ben % i == 0.