2778. Sum of Squares of Special Elements

1 min read

LeetCode Daily Problem: Sum of Squares of Special Elements

On this page

Original problem on LeetCode

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.

Constraints:

  • 1 <= nums.length == n <= 50
  • 1 <= nums[i] <= 50

Approach

A freebie — almost trivial.

python
class Solution:
    def sumOfSquares(self, nums: List[int]) -> int:
        return sum([num**2 for i, num in enumerate(nums) if len(nums) % (i+1) == 0])

Complexity

  • Time: O(n) — one pass over the array.
  • Space: O(1) extra space, excluding the output.
TagsSimulation

Back to all posts