EasyNo. 2778
Sum of Squares of Special Elements — LeetCode 2778 Python Solution
- Topics
- Array, Enumeration
- Reading time
- 2 min
- Source
- leetcode.com
The 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.
Example
- 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.
Python solution
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)Complexity
- Time
- O(n)
- Space · auxiliary
- O(1) to O(n)
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 1534 | Count Good Triplets | Easy |
| 1566 | Detect Pattern of Length M Repeated K or More Times | Easy |
| 1620 | Coordinate With Maximum Network Quality | Medium |
| 2735 | Collecting Chocolates | Medium |
| 2765 | Longest Alternating Subarray | Easy |
| 2934 | Minimum Operations to Maximize Last Elements in Arrays | Medium |
Frequently asked questions
- How hard is LeetCode 2778. Sum of Squares of Special Elements?
LeetCode 2778. Sum of Squares of Special Elements is rated Easy on LeetCode.
- What topics does LeetCode 2778. Sum of Squares of Special Elements cover?
LeetCode 2778. Sum of Squares of Special Elements is tagged Array and Enumeration on LeetCode.