October 11Easy
Sum of squares of special elements
- Array
- Enumeration
Problem
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.
Solution
class Solution:
def sumOfSquares(self, nums: List[int]) -> int:
return sum([nums[i]**2 if not len(nums) % (i + 1) else 0 for i in range(len(nums))])