2778. Sum of Squares of Special Elements LeetCode Solution

In this guide, you will get 2778. Sum of Squares of Special Elements LeetCode Solution with the best time and space complexity. The solution to Sum of Squares of Special Elements problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Sum of Squares of Special Elements solution in C++
  4. Sum of Squares of Special Elements solution in Java
  5. Sum of Squares of Special Elements solution in Python
  6. Additional Resources
2778. Sum of Squares of Special Elements LeetCode Solution image

Problem Statement of Sum of Squares of Special Elements

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.

Example 1:

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.
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
Explanation: 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.

See also  3211. Generate Binary Strings Without Adjacent Zeros LeetCode Solution

Constraints:

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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

2778. Sum of Squares of Special Elements LeetCode Solution in C++

class Solution {
 public:
  int sumOfSquares(vector<int>& nums) {
    const int n = nums.size();
    int ans = 0;

    for (int i = 0; i < n; ++i)
      if (n % (i + 1) == 0)
        ans += nums[i] * nums[i];

    return ans;
  }
};
/* code provided by PROGIEZ */

2778. Sum of Squares of Special Elements LeetCode Solution in Java

class Solution {
  public int sumOfSquares(int[] nums) {
    final int n = nums.length;
    int ans = 0;

    for (int i = 0; i < n; ++i)
      if (n % (i + 1) == 0)
        ans += nums[i] * nums[i];

    return ans;
  }
}
// code provided by PROGIEZ

2778. Sum of Squares of Special Elements LeetCode Solution in 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)
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.