LeetCode Problem Workspace

Sum of Squares of Special Elements

Calculate the sum of squares of special elements in a 1-indexed array using index divisibility checks efficiently.

category

2

Topics

code_blocks

5

Code langs

hub

3

Related

Practice Focus

Easy · Array plus Enumeration

bolt

Answer-first summary

Calculate the sum of squares of special elements in a 1-indexed array using index divisibility checks efficiently.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Array plus Enumeration

Try AiBox Copilotarrow_forward

To solve this problem, iterate through each element in the 1-indexed array and identify special elements where the index divides the array length. Square each special element and accumulate the total. This approach ensures correctness while keeping the solution straightforward and efficient for small arrays.

Problem Statement

Given a 1-indexed integer array nums of length n, an element is considered special if its index divides n exactly. Identify all such special elements and compute their squares.

Return the sum of the squares of all special elements in nums. For example, nums[1] is special if 1 divides n, nums[2] is special if 2 divides n, and so on.

Examples

Example 1

Input: nums = [1,2,3,4]

Output: 21

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

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 <= 50
  • 1 <= nums[i] <= 50

Solution Approach

Iterate and Check Divisibility

Loop through indices from 1 to n and check if n modulo i equals zero. If true, consider nums[i-1] as special and add its square to the running sum.

Accumulate Squares

For each special element identified, compute nums[i-1] * nums[i-1] and add it directly to the sum. Avoid storing all special elements unless debugging.

Return Final Sum

After processing all indices, return the accumulated sum. This ensures the solution respects the array plus enumeration pattern without extra overhead.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

Time complexity is O(n) because each index is checked once. Space complexity is O(1) since only a running sum is stored, not the list of special elements.

What Interviewers Usually Probe

  • Are you correctly identifying 1-indexed elements as special using n % i?
  • Can you explain why iterating from 1 to n covers all potential special elements?
  • Do you handle arrays where multiple indices divide n without missing any special elements?

Common Pitfalls or Variants

Common pitfalls

  • Using 0-based indexing incorrectly instead of 1-based indexing for special element checks.
  • Squaring the wrong elements by misaligning array indices with the divisor check.
  • Forgetting that n itself may be a divisor and nums[n] must be included if special.

Follow-up variants

  • Compute the product of all special elements instead of sum of squares.
  • Return the sum of cubes of special elements for a modified numerical pattern.
  • Count how many elements are special instead of summing their squares.

FAQ

What defines a special element in Sum of Squares of Special Elements?

A special element is any element nums[i] where i divides n exactly. Use n % i == 0 to check.

How do I implement the sum of squares efficiently?

Iterate indices 1 through n, check divisibility, square special elements on the fly, and accumulate the sum.

Is 0-based indexing allowed in this problem?

No, this problem uses 1-indexed arrays. Adjust your loops accordingly to avoid misaligned squares.

Can the array contain the maximum allowed length?

Yes, nums can have up to 50 elements. The O(n) approach works efficiently for all valid lengths.

Does GhostInterview help with the Array plus Enumeration pattern?

Yes, it guides identification of special elements via index-based enumeration, directly connecting to the sum of squares computation.

terminal

Solution

Solution 1

#### Python3

1
2
3
4
class Solution:
    def sumOfSquares(self, nums: List[int]) -> int:
        n = len(nums)
        return sum(x * x for i, x in enumerate(nums, 1) if n % i == 0)
Sum of Squares of Special Elements Solution: Array plus Enumeration | LeetCode #2778 Easy