Sum of Squares of Special Elements

Easy
694.

Sum of Squares of Special Elements

Sum of Squares of Special Elements is an easy coding problem on arrays, math and number theory, asked in TCS, Wipro and Capgemini interviews.

An element nums[i - 1] is special when i divides n, the length of nums. Note the 1-based index i.

Return the sum of the squares of all special elements.

Example 1

Input
nums = [1,2,3,4]
Output
21
Explanation
n is 4, and 1, 2 and 4 divide it, giving 1² + 2² + 4² = 21.

Example 2

Input
nums = [2,7,1,19,18,3]
Output
63
Explanation
n is 6, and 1, 2, 3 and 6 divide it, giving 4 + 49 + 1 + 9 = 63.

Example 3

Input
nums = [5]
Output
25

Constraints

  • 1 <= nums.length <= 50
  • 1 <= nums[i] <= 50
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {number[]} nums
 * @return {number}
 */
var sumOfSquares = function(nums) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Sum of Squares of Special Elements — Problem & Solution

Sum of Squares of Special Elements solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. An easy problem.

Sum of Squares of Special Elements is an easy coding problem on arrays, math and number theory, asked in TCS, Wipro and Capgemini interviews.

  • Difficulty: Easy
  • Topics: Arrays, Math, Number Theory
  • Asked at: TCS, Wipro, Capgemini
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

An element nums[i - 1] is special when i divides n, the length of nums. Note the 1-based index i.

Return the sum of the squares of all special elements.

Example 1

Input: nums = [1,2,3,4]
Output: 21
Explanation: n is 4, and 1, 2 and 4 divide it, giving 1² + 2² + 4² = 21.

Example 2

Input: nums = [2,7,1,19,18,3]
Output: 63
Explanation: n is 6, and 1, 2, 3 and 6 divide it, giving 4 + 49 + 1 + 9 = 63.

Example 3

Input: nums = [5]
Output: 25

Constraints

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

Hints for Sum of Squares of Special Elements

  1. Hint 1

    The divisibility test is on the position, not the value.

  2. Hint 2

    The statement is 1-based, so position i corresponds to nums[i - 1].

  3. Hint 3

    i = 1 and i = n always divide n.

How to solve Sum of Squares of Special Elements

The condition is purely positional, so loop over 1-based positions, test divisibility against the length and square the value at that position.

Approach

  1. Let n be the length of nums.
  2. For i from 1 to n, check n % i == 0.
  3. When it holds, add nums[i - 1] * nums[i - 1] to the total.

Why it works

Each position is tested exactly once against the exact predicate the statement names, so the sum ranges over precisely the special elements.

Complexity

  • Time — O(n)
  • Space — O(1)

Pitfalls

  • Using the 0-based index in the divisibility test shifts every position by one and makes i = 0 a division by zero.
  • Squaring values up to 50 across 50 positions stays well inside 32 bits, but squaring is easy to forget.

Reference solution

Python

from typing import List

def sumOfSquares(nums: List[int]) -> int:
    n = len(nums)
    return sum(nums[i - 1] ** 2 for i in range(1, n + 1) if n % i == 0)

Java

public static int sumOfSquares(int[] nums) {
    int n = nums.length, total = 0;
    for (int i = 1; i <= n; i++) {
        if (n % i == 0) total += nums[i - 1] * nums[i - 1];
    }
    return total;
}

C++

int sumOfSquares(vector<int>& nums) {
    int n = (int) nums.size(), total = 0;
    for (int i = 1; i <= n; i++) {
        if (n % i == 0) total += nums[i - 1] * nums[i - 1];
    }
    return total;
}

JavaScript

var sumOfSquares = function(nums) {
    var n = nums.length, total = 0;
    for (var i = 1; i <= n; i++) {
        if (n % i === 0) total += nums[i - 1] * nums[i - 1];
    }
    return total;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 1199 arrays problems · the whole catalogue

Learn the technique: Arrays